<%= form_tag do %>
<% end %>
<form action="/tweets/new" accept-charset="UTF-8" method="post">
<input name="utf8" type="hidden" value="✓">
<input type="hidden" name="authenticity_token" value="x9a2s9pjcNgTgS90FUwgBgylexVfnf/VnIT79bCgJ1FvXSCXSlQVe2KBknU7LQFcC7bWpGjLrX05vNUVPGuC9w==">
</form>
# やっていること
1 utf8隠しinputで
2 authenticity_token要素は クロスサイトリクエストフォージェリへの保護
https://railsguides.jp/security.html#%E3%82%AF%E3%83%AD%E3%82%B9%E3%82%B5%E3%82%A4%E3%83%88%E3%83%AA%E3%82%AF%E3%82%A8%E3%82%B9%E3%83%88%E3%83%95%E3%82%A9%E3%83%BC%E3%82%B8%E3%82%A7%E3%83%AA-csrf
formの構成要素
1"GET"メソッドを対象としたフォーム要素
2入力するものを示すラベル
3テキスト入力要素
4[送信]ボタン要素
例)
<%= form_tag("/search", method: "get") do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:q) %>
<%= submit_tag("Search") %>
<% end %>
<form accept-charset="UTF-8" action="/search" method="get"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /></div>
<label for="q">Search for:</label>
<input id="q" name="q" type="text" />
<input name="commit" type="submit" value="Search" />
</form>
気づき
input に id がname値と同じ値で生成
submitはtype
セッション
セッションid
セッションIDは、32桁の16進数のランダムな文字列です
session[:user_id] = @current_user.id User.find(session[:user_id])
パラメーター
<input id="person_address_city" name="person[address][city]" type="text" value="New York"/>
{'person' => {'address' => {'city' => 'New York'}}}
<input name="person[phone_number][]" type="text"/> <input name="person[phone_number][]" type="text"/> <input name="person[phone_number][]" type="text"/>
[]にtextの値を入れる
for_for
def new @article = Article.new end
https://www.google.co.jp/search?q=%3C%25%3D+form_for+%40article%2C+url%3A+%7Baction%3A+%22create%22%7D%2C+html%3A+%7Bclass%3A+%22nifty_form%22%7D+do+%7Cf%7C+%25%3E+%3C%25%3D+f.text_field+%3Atitle+%25%3E+%3C%25%3D+f.text_area+%3Abody%2C+size%3A+%2260x12%22+%25%3E+%3C%25%3D+f.submit+%22Create%22+%25%3E+%3C%25+end+%25%3E&oq=%3C%25%3D+form_for+%40article%2C+url%3A+%7Baction%3A+%22create%22%7D%2C+html%3A+%7Bclass%3A+%22nifty_form%22%7D+do+%7Cf%7C+%25%3E+%3C%25%3D+f.text_field+%3Atitle+%25%3E+%3C%25%3D+f.text_area+%3Abody%2C+size%3A+%2260x12%22+%25%3E+%3C%25%3D+f.submit+%22Create%22+%25%3E+%3C%25+end+%25%3E&aqs=chrome..69i57.2856j0j7&sourceid=chrome&ie=UTF-8
<form class="nifty_form" id="new_article" action="/articles" accept-charset="UTF-8" method="post"> <input name="utf8" type="hidden" value="✓" /> <input type="hidden" name="authenticity_token" value="NRkFyRWxdYNfUg7vYxLOp2SLf93lvnl+QwDWorR42Dp6yZXPhHEb6arhDOIWcqGit8jfnrPwL781/xlrzj63TA==" /> <input type="text" name="article[title]" id="article_title" /> <textarea name="article[body]" id="article_body" cols="60" rows="12"></textarea> <input type="submit" name="commit" value="Create" data-disable-with="Create" /> </form>
気づき
formのidの生成 → new_オブジェクト名
article[body]→ モデルのカラムに直接入れる
オブジェクトに紐づけているのがわかる
<%= form_for @person, url: {action: "create"} do |person_form| %> <%= person_form.text_field :name %> <%= fields_for @person.contact_detail do |contact_detail_form| %> <%= contact_detail_form.text_field :phone_number %> <% end %> <% end %>
結果
<form class="new_person" id="new_person" action="/people" accept-charset="UTF-8" method="post"> <input name="utf8" type="hidden" value="✓" /> <input type="hidden" name="authenticity_token" value="bL13x72pldyDD8bgtkjKQakJCpd4A8JdXGbfksxBDHdf1uC0kCMqe2tvVdUYfidJt0fj3ihC4NxiVHv8GVYxJA==" /> <input type="text" name="person[name]" id="person_name" /> <input type="text" name="contact_detail[phone_number]" id="contact_detail_phone_number" /> </form>
名前空間 ルーティング
コントローラを名前空間によってグループ化することもできます。最もよく使用される名前空間といえば、多数の管理用コントローラ群をまとめるAdmin::名前空間
namespace :admin do resources :articles, :comments end
form_for [:admin, @article]
@person = Person.new(city_id: 2)
# view: <%= select(:person, :city_id, [['Lisbon', 1], ['Madrid', 2], ...]) %>
<% cities_array = City.all.map { |city| [city.name, city.id] } %> <%= options_for_select(cities_array) %>
<%= options_from_collection_for_select(City.all, :id, :name) %>
アップロード
<%= form_tag({action: :upload}, multipart: true) do %> <%= file_field_tag 'picture' %> <% end %> <%= form_for @person do |f| %> <%= f.file_field :picture %> <% end %>
出力されるフォームのエンコードは 必ず "multipart/form-data"