Created
June 17, 2014 14:22
-
-
Save yohfee/07556e4c46da1819fc71 to your computer and use it in GitHub Desktop.
RE: Rails4 でお気に入りをオン・オフするトグルボタンみたいのをやってみるメモ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('#<%= dom_id @comment %>').html("<%=j render 'favorites/favorite_button', comment: @comment %>"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FavoritesController < ApplicationController | |
before_action :set_comment | |
def create | |
current_user.favorites.create(comment: @comment) | |
end | |
def destroy | |
current_user.favorites.where(comment: @comment).destroy_all | |
render 'create' | |
end | |
private | |
def set_comment | |
@comment = Comment.find(params[:comment_id]) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>Listing comments</h1> | |
<table> | |
<tbody id="comments"> | |
<% @comments.each do |comment| %> | |
<tr> | |
<td id="<%= dom_id comment %>"> | |
<%= render 'favorites/favorite_button', comment: comment %> | |
</td> | |
</tr> | |
<% end %> | |
</tbody> | |
</table> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
resources :comments do | |
resource :favorites, only: [:create, :destroy] | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
has_many :comments | |
has_many :favorites | |
def fav?(comment) | |
favorites.exists? comment: comment | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
あざーす
宿題やりますた
https://gist.github.com/CortYuming/88ccd49bc9b41e371dc0