Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
630 views
in Technique[技术] by (71.8m points)

ruby on rails - Does form_tag work with Simple_form?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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] ...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...