form_with
The following is an example using @book. form_with looks at @book as shown below and automatically decides the method when submitting.
@book | Method |
---|---|
@book=Book.new | create |
@book=Book.find(params[:id]) | update |
erb:xxx.html.erb
<%= form_with model:@book,local:true do |f| %>
<%= f.label :title %>
<%= f.text_field :Column name, class: 'form-control' %>
<%= f.submit class: 'btn' %>
<% end %>
erb:xxx.html.erb
#Input box height adjustment
#It will be the height of 5 lines
<%= f.text_area :Column name,rows:'5' class: 'form-control' %>
#Initial value of input box
#If you do not type anything, it will be "Please enter"
<%= f.text_area :Column name,placeholder:“Enter” class: 'form-control' %>
In the following, as an example, the case where @book_comment is nested in @book is shown.
erb:xxx.html.erb
<%= form_with(model:[@book, @book_comment], local: true) do |f| %>
python
<label><%= f.radio_button :feeling, "good" %>s good</label>
<label><%= f.radio_button :feeling, "numb" %>Numbness</label>
<label><%= f.radio_button :feeling, "bad" %>bad</label>
<%= f.select :feeling, [["good","good"],["numb","Numbness"],["bad","bad"]] %>
f.object
python
<%= f.number_field :age %>
hidden_field specifies the key to be stored in params and stores the value you want to put in it. I've heard a lot of freedom. If you look at params in binding.pry, you can see what's going on right away.
python
<%= f.hidden_field :user, :value => user.id %>
link_to Mainly ** show, destroy, edit ** link templates
erb:xxx.html.erb
#Destroy (with confirmation message)
<%= link_to 'Destroy', book_path(@book), method: :delete, data: { confirm: 'Do you really want to erase it?' }, class: "btn btn-sm btn-danger destroy_book_#{@book.id}"%>
If you have a partial template in the same location as this view file, you can use'form' In the partial template, use book. At the end is the work of passing a value to @ book => book.
python
<%= render 'book/form', book: @book %>
python
redirect_to book_path
redirect_to request.referer
python
private
def book_params
params.require(:book).permit(:name, :price, :opinion)
end
python
validates :title, presence: true
python
class User < ApplicationRecord
has_many :posts
end
class Post < ApplicationRecord
belongs_to :user
end
In the following example, the list page of Book is taken as an example.
erb:xxx.html.erb
<% @books.each do |ebifry| %>
<tr>
<td>ebifry.title</td>
<td>ebifry.opinion</td>
</tr>
<% end %>
erb:xxx.html.erb
<table class='table table-hover table-inverse'>
<thead>
<tr>
<th>Book title</th>
<th>Impressions of the book</th>
</tr>
</thead>
<tbody>
<% @books.each do |book| %>
<tr>
<td>book.title</td>
<td>book.opinion</td>
</tr>
<% end %>
</tbody>
</table>
If you want to add an edit button or delete button for each book
erb:xxx.html.erb
<td>book.opinion</td>Also below, insert the following:
-----------------------------------------
<td>
<%= link_to 'Destroy', book_path(book), method: :delete, data: { confirm: 'Do you really want to erase it?' }, class: "btn btn-sm btn-danger destroy_book_#{book.id}"%>
</td>