Does form_tag work with Simple_form?

半世苍凉 提交于 2019-11-27 20:01:31

问题


I have a form that is using form_tag and not sure how to use it with the simple_form gem. This is how my form looks:

<%= form_tag create_multiple_prices_path, :method => :post do %>

  <% @prices.each_with_index do |price, index| %>
    <%= fields_for "prices[#{index}]", price do |up| %>
      <%= render "fields", :f => up %>
    <% end %>
  <% end %>

  <%= submit_tag "Done" %>
<% end %>

Can it be done? How would a form_tag change to use simple_form correctly? What about when using it with fields_for? A Newbie could use some help.

Thank you.


回答1:


simple_form is a wrapper around form_for, not form_tag. You can use simple_form_for instead of form_for, but form_tag just creates <form> tags with specified content, it is not relevant to simple form.




回答2:


You can use simple_form even if you aren't creating a form that's tied to a model.

Take this signin form as an example:

<%= simple_form_for :signin, { url: signin_path } do |f| %>
  <%= f.input :email %>
  <%= f.input :password %>
  <%= f.button :submit, "Sign In" %>
<% end %>

That will generate params like the following:

{
  ...
  "signin" => {
    "email"=>"test@test.com",
    "password"=>"[FILTERED]"},
    "commit"=>"Sign In"
   }
 }

In your controller you can reference the form fields using:

params[:signin][:email] ...



回答3:


You can avoid use of

params[:signin][:email]

using

<%= f.input :email, input_html: { name: "email" } %>

so

params[:email]



回答4:


like @barelyknown said You can use simple_form even without model You can also use field_for or simple_field_for

<%= simple_form_for :transaction_limits, {url: create_multiple_prices_path, method: :post} do |f| %>
  <% @prices.each_with_index do |price, index| %>
    <%= f.fields_for "prices[#{index}]", price do |up| %>
      <%= render "fields", :f => up %>
    <% end %>
  <% end %>

  <%= f.submit "button" %>
<% end %>


来源:https://stackoverflow.com/questions/9342277/does-form-tag-work-with-simple-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!