LoginSignup
8
9

More than 5 years have passed since last update.

[rails]form_for / form_tagの違い

Last updated at Posted at 2015-06-12

 まとめ

form_for → modelに基づいたformを作るときに使う。
form_tag → modelに基づかないformを作るときに使う。


 <% form_for(@book) do |f| %>
    <%= f.label :name %>
    <%= f.text_field :name %>
    <%= f.submit %>
 <% end %>

bookコントローラーのcreateメソッドにpostします。

  Parameters: {
    "utf8"=>"✓", 
    "authenticity_token"=>"~~~~", 
    "book"=>{
        "name"=>"ノルウェイの森"
        }
    }

Book.create(book)で保存できます。

 <% form_tag{ :controller => "book", :action => "create"} do %>
    <%= label_tag :name %>
    <%= text_field_tag :name %>
    <%= submit_tag %>
 <% end %>

bookコントローラーのcreateメソッドにpostします。

8
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
9