-Version 10.15.3 ・ Ruby ruby 2.6.3p62 ・ Rails 6.0.3.2
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 ...
Here, we will proceed from form creation in the view.
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>
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"
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>.
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 %>
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
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.
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.
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
app/controllers/Any_controller.rb
‥
def create
params[:content]
params[:title]
redirect_to("/Controller name/Action name")
end
Recall that it was instantiated and created with the rails console </ strong>. Enter to create a Post instance in place of params above.
app/controllers/Any_controller.rb
‥
def create
@post = Post.new(content: params[:content])
@post.save
@post = Post.new(title: params[:title])
@post.save
redirect_to("/posts/index")
end
Don't forget @ post.save </ strong>. Up to this point, the posted data will be displayed if possible.
The title and text are reflected.
The above files are published on github. If you are interested, please download it. → Github
The above is the reflection of the posted content from the form. It's quite an application-like mechanism, and the tension goes up. Now that the posting function has been created, I would like to add a mechanism such as "delete / edit" from now on.
Books: <a href="https://www.amazon.co.jp/%E3%81%9F%E3%81%AE%E3%81%97%E3%81%84Ruby-%E7%AC%AC6 % E7% 89% 88-Informatics-IDEA-% E9% AB% 98% E6% A9% 8B / dp / 4797399848 / ref = sr_1_1? __mk_ja_JP =% E3% 82% AB% E3% 82% BF% E3% 82 % AB% E3% 83% 8A & dchild = 1 & keywords =% E3% 81% 9F% E3% 81% AE% E3% 81% 97% E3% 81% 84Ruby +% E7% AC% AC6% E7% 89% 88 & qid = 1600088731 & s = books & sr = 1-1 "target =" blank "> Fun Ruby 6th Edition
Also, there is a link to the Twitter portfolio, so if you are interested, Please connect. I would be very happy to have friends who can share programming learning.
Recommended Posts