Rails 3.0:form_tag如何将此form_for转换为form_tag。

时间:2012-04-20 10:28:29

标签: ruby-on-rails-3

这是我的应用程序的登录页面,但是为用户选择的角色没有反映在数据库中,因此我想尝试将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 %>

1 个答案:

答案 0 :(得分: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 %>