投票機能(いいね機能)を二つつけたいです。acts_as_votableのgemを使っているのですが、投稿にいいね機能、それに対するコメント自体にも投票機能をつけたいのですがどうしたらいいのでしょうか?「投稿にいいね機能」はできたのですが、「それに対するコメント自体にも投票機能」ができません。以下のようなエラーが出ます。
以下、試したコードです。
songに投票とコメントをネストさせて、そのコメントにさらに投票機能をネストさせました。
↓routes.rb
resources :comments devise_for :users resources :songs do member do put "like", to: "songs#upvote" end resources :comments do member do put "like", to: "comments#upvote" put "dislike", to: "comments#downvote" end end end root to: "songs#index"
↓コメントコントローラー
def upvote @comment = Comment.find(params[:id]) @comment.upvote_by current_user redirect_to :back end def downvote @comment = Comment.find(params[:id]) @comment.downvote_by current_user redirect_to :back end private def set_comment @comment = Comment.find(params[:id]) end def comment_params params.require(:comment).permit(:song_id, :body, :user_id, :image) end
↓ビュー
<div class="btn-group pull-right"> <%= link_to like_song_comment_path(@comment), method: :put, class: "btn btn-default btn-sm" do %> <span class="glyphicon glyphicon-chevron-up"></span> Upvote <%= @comment.get_upvotes.size %> <% end %> <%= link_to dislike_song_comment_path(@comment), method: :put, class: "btn btn-default btn-sm" do %> <span class="glyphicon glyphicon-chevron-down"> Downvote <%= @comment.get_downvotes.size %> <% end %> </div>
↓rake routes
comments GET /comments(.:format) comments#index POST /comments(.:format) comments#create new_comment GET /comments/new(.:format) comments#new edit_comment GET /comments/:id/edit(.:format) comments#edit comment GET /comments/:id(.:format) comments#show PATCH /comments/:id(.:format) comments#update PUT /comments/:id(.:format) comments#update DELETE /comments/:id(.:format) comments#destroy new_user_session GET /users/sign_in(.:format) devise/sessions#new user_session POST /users/sign_in(.:format) devise/sessions#create destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy user_password POST /users/password(.:format) devise/passwords#create new_user_password GET /users/password/new(.:format) devise/passwords#new edit_user_password GET /users/password/edit(.:format) devise/passwords#edit PATCH /users/password(.:format) devise/passwords#update PUT /users/password(.:format) devise/passwords#update cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel user_registration POST /users(.:format) devise/registrations#create new_user_registration GET /users/sign_up(.:format) devise/registrations#new edit_user_registration GET /users/edit(.:format) devise/registrations#edit PATCH /users(.:format) devise/registrations#update PUT /users(.:format) devise/registrations#update DELETE /users(.:format) devise/registrations#destroy like_song PUT /songs/:id/like(.:format) songs#upvote like_song_comment PUT /songs/:song_id/comments/:id/like(.:format) comments#upvote dislike_song_comment PUT /songs/:song_id/comments/:id/dislike(.:format) comments#downvote song_comments GET /songs/:song_id/comments(.:format) comments#index POST /songs/:song_id/comments(.:format) comments#create new_song_comment GET /songs/:song_id/comments/new(.:format) comments#new edit_song_comment GET /songs/:song_id/comments/:id/edit(.:format) comments#edit song_comment GET /songs/:song_id/comments/:id(.:format) comments#show PATCH /songs/:song_id/comments/:id(.:format) comments#update PUT /songs/:song_id/comments/:id(.:format) comments#update DELETE /songs/:song_id/comments/:id(.:format) comments#destroy songs GET /songs(.:format) songs#index POST /songs(.:format) songs#create new_song GET /songs/new(.:format) songs#new edit_song GET /songs/:id/edit(.:format) songs#edit song GET /songs/:id(.:format) songs#show PATCH /songs/:id(.:format) songs#update PUT /songs/:id(.:format) songs#update DELETE /songs/:id(.:format) songs#destroy root GET / songs#index
あなたの回答
tips
プレビュー