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
%>
いいね機能実装
- Likesテーブルを作成
- 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を使う(例)
- gemファイルに記述
-
bundle install
で内容を実行has_secure_password
が使えるようになる。 ただmodelに貼り付ければ
カラム名を password_digest
にする必要がある。
ただし、インスタンス作成からアクセスする際は password
で大丈夫のよう。
has_secure_passwordをmodelに記述すると、authenticateメソッドが有効になる
authenticateメソッドは渡された引数を暗号化し、password_digestの値と一致するかどうかを判定してくれる。