0
0

More than 3 years have passed since last update.

Railsチュートリアル学習メモ3

Last updated at Posted at 2019-12-02

renderの中身はルーティングパスじゃなくてcontrollerの階層を記述する

render("users/login_form")

viewで <%= @user.id %> などとやらずにechoする方法

      <% 
      if @user.id == @current_user.id
        concat link_to("編集", "/users/#{@user.id}/edit")
      end
      %>

concatを使う。なんでputsとかprintとかできないんだろ。

簡単にループ

      <%
        10.times do
          concat "cat"
        end
      %>

いいね機能実装

  1. Likesテーブルを作成
  2. Likeモデルを作成して、Viewからレコードを追加/削除機能を作成

FontAwesomeの使用

view/lauout/application.html.erb
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

link_toの表示ラベル部分をHTML要素にするための書き方

<%= link_to("/likes/#{@post.id}/destroy", {method: "post"}) do%>
<span class="fa fa-heart like-btn"></span>
<% end %>

第一引数は不要

レコードをカウントする

@likes_count = Like.where(post_id: params[:post_id]).count

パスワードのハッシュ化はgemのbcryptを使う(例)

  1. gemファイルに記述
  2. bundle install で内容を実行 has_secure_password が使えるようになる。 ただmodelに貼り付ければ

カラム名を password_digest にする必要がある。

ただし、インスタンス作成からアクセスする際は password で大丈夫のよう。

has_secure_passwordをmodelに記述すると、authenticateメソッドが有効になる

authenticateメソッドは渡された引数を暗号化し、password_digestの値と一致するかどうかを判定してくれる。

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