0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

備忘録:某スクールで使うRails Viewのチートシート(作成中)

Last updated at Posted at 2020-12-19

form_with

以下は、@bookを用いて例を示します。
form_withは、以下のように@bookを見て、submitした時のメソッドを自動で決めてくれる。

筆者つぶやき 入力フォームは、**部分テンプレートで作成しておけば、便利。** edit画面でもnew画面でも流用できる。 form_withは、@bookの中身を見て判断してくれるからだ。
@book メソッド
@book=Book.new create
@book=Book.find(params[:id]) update
xxx.html.erb
<%= form_with model:@book,local:true do |f| %>

    <%= f.label :title %>
    <%= f.text_field :カラム名, class: 'form-control' %>

    <%= f.submit class: 'btn' %>

<% end %>

form_with 追加オプション

テキストボックス関連
xxx.html.erb

 #入力ボックスの高さ調整  
  #5行分の高さになる
<%= f.text_area  :カラム名,rows:'5' class: 'form-control' %>

 #入力ボックスの初期値
  #なにも打ち込まなければ「入力してください」となる
<%= f.text_area  :カラム名,placeholder: 入力してください class: 'form-control' %>

ネストされているモデルをフォームに書く場合

以下では、例として、@book_comment@bookにネストさている場合を示す。

筆者つぶやき form_withをよく見てみると、URLを一切指定していない。(指定することも可能だが。) つまり、「model:@book」の部分で判断して、URLを発行してくれていることになる。 ネストすれば、URLは"/book/3/book_comment"とかになる。 このことを分かってもらうために、 例えば@book_commentの投稿にも、@bookの存在を同時にform_withに教えてあげないといけないのか。
xxx.html.erb
<%= form_with(model:[@book, @book_comment], local: true) do |f| %>
radioボタン
<label><%= f.radio_button :feeling, "good" %>s良い</label>
<label><%= f.radio_button :feeling, "numb" %>無感覚</label>
<label><%= f.radio_button :feeling, "bad" %>悪い</label>


<%= f.select :feeling, [["good","良い"],["numb","無感覚"],["bad","悪い"]] %>

f.object

<%= f.number_field :age %>

hidden_fieldは、paramsに格納されるときのキーを指定し、そこに入れたい値を格納する。
自由度がかなり聞いている。binding.pryでparamsを見れば、どうなってるかすぐ分かる。

<%= f.hidden_field :user, :value => user.id %>

link_to

主に、show、destroy、editのリンクの雛形

xxx.html.erb



  #Destroy(確認メッセージ付き)
<%= link_to 'Destroy', book_path(@book), method: :delete, data: { confirm: '本当に消しますか?' }, class: "btn btn-sm btn-danger destroy_book_#{@book.id}"%>



部分テンプレート

このviewファイルと同じ場所に部分テンプレートがあれば、'form'で良い
部分テンプレート内では、bookを使用する。末尾は@book=>bookに値を渡す作業。


<%= render 'book/form', book: @book %>

リダイレクト

redirect_to book_path
redirect_to request.referer

ストロングパラメータ


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

バリデーション

validates :title, presence: true

1対多関係の構築

class User < ApplicationRecord
    has_many :posts, dependent: :destroy
end

class Post < ApplicationRecord
    belongs_to :user
end

Each文とTable

以下例では、Bookの一覧ページを例とする。

筆者つぶやき 以下のように書いたところで問題はない。 自分で名前をつけて良いところか、modelを元に命名しないといけないところかを意識したい。 もっというと、@ebifry=Book.allでも一応成り立つ。
xxx.html.erb

    <% @books.each do |ebifry| %>
       <tr>
         <td>ebifry.title</td>
         <td>ebifry.opinion</td>
       </tr>
    <% end %>
xxx.html.erb
<table class='table table-hover table-inverse'>
 
  <thead>
     <tr>
       <th>本のタイトル</th>
       <th>本の感想</th>
     </tr>
  </thead>

  <tbody>
     <%  @books.each do |book|  %>
       <tr>
         <td>book.title</td>
         <td>book.opinion</td>
       </tr>
     <% end %>
  </tbody>

</table>

本ごとに編集ボタンや削除ボタンを付けたいなら

xxx.html.erb
<td>book.opinion</td>の下にでも、以下を挿入する。
-----------------------------------------
<td>
<%= link_to 'Destroy', book_path(book), method: :delete, data: { confirm: '本当に消しますか?' }, class: "btn btn-sm btn-danger destroy_book_#{book.id}"%>
</td>

devise

今回は、deviseでログインした時に、session情報に会社名を保存したい。
以下のように、deviseコントローラに機能を追加する。
destroyはしなくていい。
なぜならdeviseで勝手にsessionは全削除してくれる。

devise/session.rb

  # POST /resource/sign_in
  # def create
  #  super
  # end

  # ↓ コメントアウトを解除して、付け加えるだけ

def create
    super
    session[:company]=current_user
  end

routes

route

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?