Consideration of Routing of form_with helper method

Purpose of this post ・ I will write it as minutes to make a note of Rails operation. ・ It is a continuation of [Rails Review 1](https://qiita.com/naoto911/items/f46b35f84cc80f59cac3) and [Rails Review 2](https://qiita.com/naoto911/items/f907348510a5dffb0a2f) that I wrote earlier. .. -Also, since this time I wrote it at the hypothesis stage, there is a possibility that there is an error in recognition.

Teaching materials used for learning Udemy's ["Introduction to Ruby on Rails-Learn Ruby and Rails from the Basics and Publish Web Applications to the Net"](https://www.udemy.com/course/the-ultimate-ruby-on-rails- I used bootcamp /) as a teaching material.

○ How to use form_with

qiita.rb


link_to 'Link name', 'Prefix name+ _path'

・ Automatically creates html (creates href) ・ Mainly use the following three

○text_field ⇨ User-written space ⇨ Describe the content that fits in one line

○text_area ⇨ User-written space ⇨ Describes more detailed content than form_with ○submit ⇨ Processing when the user presses to access another URL, such as a submit button or enter

ruby: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 "Send", class: "btn btn-primary" %>

<% end %>

[Explanation] ○form_with model:@question ⇨ The explanation here is complicated, so I will explain in detail below.

○local:true ⇨ Disable asynchronous communication form ⇨ Details are unknown and need to be investigated separately ...

○do |f| ⇨ Store the hashes in @question separately

○f.text_field :name ⇨ Extract and display the value that hits: name from the separated items.

○f.text_area :content ⇨ Extract and display the value that hits: content from the pieces

○ f.submit "submit" ⇨ Display "Send" on the button

[Where do you specify the path after executing the submit method?]

The following two things are important here ① Submit is sent by POST method </ font> (2) How many model names does the form (form_with method use page) include? -If there is only one model, specify the URL in the Prefix that contains only model name + s </ font> as a character string. -If there are two or more models, specify the URL in the Prefix that contains only model name 1_model name 2 + s </ font> as a character string. ・ * (Defined as model name 1, model name 2, ..., model name n from the side closer to root of path)

○ Confirm with 2 patterns of examples during the following Routing

                  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

○ Assuming that form_with is displayed in the URL of / questinos / new (Pattern 1)

[About ①] -Submit is sent by the POST method -Destination path candidates are narrowed down to POST among Prefixn

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

[About ②] ・ Focus on the URL of the page where the form is displayed -Form_with is displayed in the URL of / questinos / new ・ Includes one question -For Prefix, specify the URL for Prefix that contains only model name + s as a character string. ・ Therefore, specify questions


○ It is assumed that form_with is displayed in the URL of / questions /: questions / id / answers / new (Pattern 2)

[About ①] ・ Similar to pattern 1, narrow down by POST method

[About ②] ・ Focus on the URL of the page where the form is displayed -Form_with is displayed in the URL of / questions /: questions_id / answers / new ・ There are two models included, question and answer. -For Prefix, specify the URL for Prefix that contains only model name 1_model name 2 + s as a character string. ・ Therefore, specify question_answers


○ Other assumptions ・ The above two examples are all explained focusing on form_with from the #new action. -Actually, form_with is also used from the #edit action. ・ However, if you judge by the above Logic, Tsuji will match.

○ Summary ・ Summarized how to determine the destination url of form_with -The above explanation assumes a so-called nested type in which models are nested. ・ Nesting will be summarized separately at a later date.