LoginSignup
1
0

More than 3 years have passed since last update.

【Rails】form_withのRoutingについての考察

Last updated at Posted at 2020-11-09

本投稿の目的

・Railsの操作をメモするために議事録として書いていきます。
・仮説の段階で書いているので認識に誤りがある可能性があります。


学習に使った教材

Udemyの "はじめてのRuby on Rails入門-RubyとRailsを基礎から学びWebアプリケーションをネットに公開しよう" を教材として使用しました。


○form_withの使い方

・htmlを自動で作成してくれる (href作成をしてくれる)
・主に以下の3つを使用する

○text_field
⇨ユーザーが記載するスペース
⇨1行で収まる程度の内容を記載

○text_area
⇨ユーザーが記載するスペース
⇨form_withに比べて詳細な内容を記載
○submit
⇨送信ボタンや決定など,ユーザーが押して別のURLへアクセスする際の処理

qiita.html.erb
<%= form_with model:@question, local:true do |f| %>

<%= f.text_field :name, class: "form-control" %>

<%= f.text_area :content, class: "form-control" %>

<%= f.submit "送信", class: "btn btn-primary" %>

<% end %>

【解説】
○form_with model:@question
⇨ここの説明がくそ複雑なので下で詳細を説明します

○local:true
⇨非同期通信フォームを無効化
⇨詳細は不明のため別途調査が必要です...

○do |f|
@question 内のハッシュをバラバラに格納していく

○f.text_field :name
⇨バラバラにした中から,:name にヒットする値に入力値を格納

○f.text_area :content
⇨バラバラにした中から,:content にヒットする値に入力値を格納

○f.submit "送信"
⇨ボタンに"送信"と表示させる

【submitメソッドを実行後のpathはどこで指定するのか?】

ここで重要なことは以下の2つ
①submitはPOSTメソッドで送信
②フォーム(form_withメソッド使用ページ)はmodel名をいくつ含んでいるか?
・modelが1つなら model名+s のみを文字列として含むPrefixにURLを指定
・modelが2つ以上なら model名1_model名2+s のみを文字列として含むPrefixにURLを指定
・*(pathのrootに近い側からmodel名1,model名2,...,model名n と定義)

○以下のようなRouting時に2パターンの例題で確認

                  Prefix  Verb   URI Pattern                                                                              Controller#Action
             answers_edit GET    /answers/edit(.:format)                                                                  answers#edit
                     root GET    /                                                                                        questions#index
         question_answers GET    /questions/:question_id/answers(.:format)                                                answers#index
                          POST   /questions/:question_id/answers(.:format)                                                answers#create
      new_question_answer GET    /questions/:question_id/answers/new(.:format)                                            answers#new
     edit_question_answer GET    /questions/:question_id/answers/:id/edit(.:format)                                       answers#edit
          question_answer GET    /questions/:question_id/answers/:id(.:format)                                            answers#show
                          PATCH  /questions/:question_id/answers/:id(.:format)                                            answers#update
                          PUT    /questions/:question_id/answers/:id(.:format)                                            answers#update
                          DELETE /questions/:question_id/answers/:id(.:format)                                            answers#destroy
                questions GET    /questions(.:format)                                                                     questions#index
                          POST   /questions(.:format)                                                                     questions#create
             new_question GET    /questions/new(.:format)                                                                 questions#new
            edit_question GET    /questions/:id/edit(.:format)                                                            questions#edit
                 question GET    /questions/:id(.:format)                                                                 questions#show
                          PATCH  /questions/:id(.:format)                                                                 questions#update
                          PUT    /questions/:id(.:format)                                                                 questions#update
                          DELETE /questions/:id(.:format)                                                                 questions#destroy


             answers_edit GET    /answers/edit(.:format)                                                                  answers#edit
                     root GET    /                                                                                        questions#index
         question_answers GET    /questions/:question_id/answers(.:format)                                                answers#index
                          POST   /questions/:question_id/answers(.:format)                                                answers#create
      new_question_answer GET    /questions/:question_id/answers/new(.:format)                                            answers#new
     edit_question_answer GET    /questions/:question_id/answers/:id/edit(.:format)                                       answers#edit
          question_answer GET    /questions/:question_id/answers/:id(.:format)                                            answers#show

○form_withは /questinos/new のURLに表示されている と想定 (パターン1)

【①について】
・submitはPOSTメソッドで送信
・送信先path候補は,PrefixnのうちPOSTのものに絞られる

                  Prefix  Verb   URI Pattern                                                                              Controller#Action
         question_answers POST   /questions/:question_id/answers(.:format)                                                answers#create
                questions POST   /questions(.:format)                                                                     questions#create

【②について】
・フォームが表示されるページのURLに着目
・form_withは /questinos/new のURLに表示されている
・含まれるmodelは question 1つ
・Prefixは model名+s のみを文字列として含むPrefixにURLを指定
・よって questions を指定


○form_withは /questions/:question_id/answers/new のURLに表示されている と想定 (パターン2)

【①について】
・パターン1と同様にPOSTメソッドで絞り込む

【②について】
・フォームが表示されるページのURLに着目
・form_withは /questions/:question_id/answers/new のURLに表示されている
・含まれるmodelは question と answer の2つ
・Prefixは model名1_model名2+s のみを文字列として含むPrefixにURLを指定
・よって question_answers を指定


○その他の想定

・上の2つの例はすべて #new アクションからのform_with に着目して説明した
・実際には, #edit アクションからもform_with を使用している
・しかし,上記のLogicで判定しておけば辻褄が合うはず


○まとめ

・form_withの送信先urlの判定の仕方についてまとめた
・上記の説明には,modelが入れ子になったいわゆるネスト型を想定した
・ネストについては後日,別途まとめる

1
0
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
0