1
2

More than 5 years have passed since last update.

フォームで配列を扱う方法

Posted at

フォーム

form
<%= form_tag :action => :edit_tag, :id => @id do %>
  Tag1 : <%= text_field_tag 'tags[]' %><br />
  Tag2 : <%= text_field_tag 'tags[]' %><br />
  Tag3 : <%= text_field_tag 'tags[]' %><br />
  Tag4 : <%= text_field_tag 'tags[]' %><br />
<%= submit_tag %>
<% end %>

送信すると以下のように格納される

params = { :tags => ['tag1', 'tag2', 'tag3', 'tag4'] }

ハッシュ

form
<% form_tag :action => :create do -%>
  <%= text_field_tag 'post[title]' %><br />
  <%= text_area_tag  'post[body]' %><br />
  <%= submit_tag %>
<% end -%>

送信すると以下のように格納される

params = {
  :post => {
    :title => "タイトル",
    :body => "本文"
  }
}
1
2
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
1
2