Note: Rails View cheat sheet used at a certain school (under construction)

form_with

The following is an example using @book. form_with looks at @book as shown below and automatically decides the method when submitting.

Author's tweet It is convenient if the input form is created with ** partial template. ** ** It can be used on both the edit screen and the new screen. This is because form_with makes a judgment by looking at the contents of @book.
@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 %>

form_with additional options

Text box related

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' %>

When writing a nested model on a form

In the following, as an example, the case where @book_comment is nested in @book is shown.

Author's tweet If you take a closer look at form_with, you can see that no URL is specified. (Although it is possible to specify.) In other words, the URL is issued based on the "model: @ book" part. If nested, the URL will be something like "/ book/3/book_comment". To help you understand this For example, do I have to tell form_with the existence of @book at the same time when posting @book_comment?

erb:xxx.html.erb


<%= form_with(model:[@book, @book_comment], local: true) do |f| %>
radio button

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}"%>



Partial template

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 %>

redirect

python


redirect_to book_path
redirect_to request.referer

Strong parameters

python



private
def book_params
  params.require(:book).permit(:name, :price, :opinion)
end

Validation

python


validates :title, presence: true

Building a one-to-many relationship

python


class User < ApplicationRecord
    has_many :posts
end

class Post < ApplicationRecord
    belongs_to :user
end

Each statement and Table

In the following example, the list page of Book is taken as an example.

Author's tweet There is no problem if you write as follows. I want to be aware of whether I can name it myself or I have to name it based on the model. In other words, @ ebifry = Book.all also holds true.

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>

Recommended Posts

Note: Rails View cheat sheet used at a certain school (under construction)
Note: Cheat sheet when creating Rails Vue app
Rails Tutorial cheat sheet
A cheat sheet for Java experienced people to learn Ruby (rails)