2012-04-20 47 views
0

這是我的應用程序的登錄頁面,但爲用戶選擇的角色未反映在數據庫中,所以我想嘗試將form_for更改爲form_tag以查看數據是否已提交。Rails 3.0:form_tag如何將這個form_for轉換爲form_tag。

<%= form_for(@user) do |f| %> 
     <% if @user.errors.any? %> 
     <div id="error_explanation"> 
      <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2> 
      <ul> 
      <% @user.errors.full_messages.each do |msg| %> 
       <li><%= msg %></li> 
      <% end %> 
      </ul> 
     </div> 
     <% end %> 
     <div class="field"> 
     <%= f.label :email %><br /> 
     <%= f.text_field :email %> 

     </div> 
     <div class="field"> 
     <%= f.label :username %><br /> 
     <%= f.text_field :username %> 
     </div> 
     <% if @current_method == "new" %> 
     <div class="field"> 
      <%= f.label :password %><br /> 
      <%= f.password_field :password %> 
     </div> 
     <div class="field"> 
      <%= f.label :password_confirmation %><br /> 
      <%= f.password_field :password_confirmation %> 
     </div> 
     <% end %> 
     <% for role in Role.find(:all) %> 
     <div> 
      <%= check_box_tag "user[role_ids][]", role.id, @user.roles.include?(role) %> 
      <%= role.name %> 
     </div> 
     <% end %> 
     <div class="actions"> 
     <%= f.submit %> 
     </div> 
    <% end %> 

回答

0
<%= form_tag users_path, methods=> post:do %> 
    <% if @user.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2> 
     <ul> 
     <% @user.errors.full_messages.each do |msg| %> 
      <li><%= msg %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 
    <div class="field"> 
    <%= label_tag 'email' %><br /> 
    <%= text_field_tag :email, params[:email], :placeholder => "Email" %> 
    </div> 
    <div class="field"> 
    <%= label_tag 'username' %><br /> 
    <%= text_field_tag :username, params[:username], :placeholder => "Username" %> 
    </div> 
    <% if @current_method == "new" %> 
    <div class="field"> 
     <%= label_tag :password %><br /> 
     <%= password_field_tag 'password' %> 
    </div> 
    <div class="field"> 
     <%= label_tag :password_confirmation %><br /> 
     <%= f.password_field_tag 'password_confirmation' %> 
    </div> 
    <% end %> 
    <% for role in Role.find(:all) %> 
    <div> 
     <%= check_box_tag "user[role_ids][]", role.id, @user.roles.include?(role) %> 
     <%= role.name %> 
    </div> 
    <% end %> 
    <div class="actions"> 
<%= submit_tag 'submit' %> 
    </div> 
<% end %>