Skip to content

Instantly share code, notes, and snippets.

@CortYuming
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CortYuming/88ccd49bc9b41e371dc0 to your computer and use it in GitHub Desktop.
Save CortYuming/88ccd49bc9b41e371dc0 to your computer and use it in GitHub Desktop.
RE: 『RE: Rails4 でお気に入りをオン・オフするトグルボタンみたいのをやってみるメモ』の宿題
<%= link_to "Favorite", fav_comment_path(comment), method: :post, remote: true, class: "btn btn-xs #{current_user.fav?(comment) ? 'btn-warning' : 'btn-default' }" %>
def fav
@comment = Comment.find(params[:id])
if @comment.favorites.blank?
current_user.favorites.create(comment: @comment)
else
current_user.favorites.where(comment: @comment).destroy_all
end
end
$('#<%= dom_id @comment %>').html("<%=j render 'favorites/favorite_button', comment: @comment %>");
resources :comments do
member do
post :fav
end
end
@CortYuming
Copy link
Author

元ネタ
『Rails4 でお気に入りをオン・オフするトグルボタンみたいのをやってみるメモ』
http://d.hatena.ne.jp/CortYuming/20140617/p1

返答
『RE: Rails4 でお気に入りをオン・オフするトグルボタンみたいのをやってみるメモ』
https://gist.github.com/yohfee/07556e4c46da1819fc71
これの宿題やってみますた。
あとんす。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment