(Ruby on Rails6) Reflecting the posted content from the form

Machine specs

-Version 10.15.3 ・ Ruby ruby 2.6.3p62 ・ Rails 6.0.3.2

Preface

In above , I recorded how to display the database that got the id of the database. Here, we record as a memorandum how to reflect the contents of the database posted from the form in the view. Rooding is complicated and difficult ...

Reflecting the posted content from the form

Here, we will proceed from form creation in the view.

Creating a form screen

Create a simple form for your view.

erb:app/views/Any/Any.htme.erb


<div class="form">
  <label>title: </label>
  <input type="text">
  <br>
  <label>text: </label>
  <textarea></textarea>
  <input type="submit" value="Post">
</div>

Creating an action

Create the previous form ↓ I have set submit </ strong>. In order to save the data entered in the form to the database, it is necessary to go through the action (controller) when clicking submit.

erb:app/views/Any/Any.htme.erb


  <input type="submit" value="Post">

Fill in the routes below

config/routes.rb


 ‥
  post "Controller name/Action name" => "Controller name#Action name"

Example ↓

config/routes.rb


 ‥
  post "posts/create" => "posts#create"

About elements

config/routes.rb


  get "Controller name/Action name" => "Controller name#Action name"
  post "Controller name/Action name" => "Controller name#Action name"

There are get ・ post </ strong> in the routes of ↑. If it is the page routing setting, get is fine. However, if you want to get the form value </ strong>, you need to post </ strong>.

submit to action

You must set the form_tag method </ strong> to submit data to the form.

(form_tag method) Before setting ↓

rb:app/views/Any.html.erb


<div class="form">
  <label>title: </label>
  <input type="text">
  <br>
  <label>text: </label>
  <textarea></textarea>
  <input type="submit" value="Post">
</div>

(form_tag method) After setting ↓

rb:app/views/Any.html.erb


<%= form_tag("/Controller name/Action name") do %>
 <div class="form">
   <label>title: </label>
   <input type="text">
   <br>
   <label>text: </label>
   <textarea></textarea>
   <input type="submit" value="Post">
 </div>
<% end %>

I set it with <% = form_tag ("/ posts / create") do%> ‥ <% end%> </ strong>. Also, the form_tag method becomes ↓.

rb:app/views/Any.html.erb


<%= form_tag("/Controller name/Action name") do %>
processing
<% end %>

Redirect settings

In the work up to now ・ Form creation -Submitting from form to action The we. Here, in order to display the sent data, set to redirect to the corresponding (list) page.

Use the redirect_to method </ strong> to set up a redirect.

app/controllers/Any_controller.rb


‥

def create
    redirect_to("/Controller name/Action name")
end

Example ↓

app/controllers/Any_controller.rb


‥

def create
    redirect_to("/posts/index")
end

Send and save the input data to the database

Check the form you set earlier.

rb:app/views/Any.html.erb


<%= form_tag("/Controller name/Action name") do %>
 <div class="form">
   <label>title: </label>
   <input type="text">
   <br>
   <label>text: </label>
   <textarea></textarea>
   <input type="submit" value="Post">
 </div>
<% end %>

Input data cannot be sent for the two ↓ described here as they are.

  • 2 (input / textarea)

rb:app/views/Any.html.erb


<%= form_tag("/Controller name/Action name") do %>
   <input type="text">
   <textarea></textarea>
<% end %>

Set the name attribute </ strong> to allow sending.

↓ setting of name attribute

rb:app/views/Any.html.erb


<%= form_tag("/Controller name/Action name") do %>
   <input name="title" type="text">
   <textarea name="content"></textarea>
<% end %>

For the name attribute setting, set the column name when creating the database. I have set "title and text".

By setting the name attribute and sending it, a hash with the value of the name attribute as the key is sent to the action.

Next, let's associate the "name attribute" with the action controller.

name attribute setting and variable "params"

In the previous form, I set the name attribute so that it can be sent to the action of the controller. Then set the controller to params </ strong> to receive the data submitted in the form. params </ strong> are parameters that receive values such as form data and link_to. In the above Views, I linked the data to the