Я получаю NoMethodError, что я не могу понять, это говорит undefined method 'has_liked?'
. У меня есть «идеи» crud, у которых есть «комментарии», и я пытаюсь добавить способность любить/не любить комментарии.Rails: неопределенный метод `has_liked? '
NoMethodError in Comments#new
на этой линии здесь <% if user_signed_in? && current_user.has_liked?(comment) %>
Вот мой comments_controller.rb
class CommentsController < ApplicationController
def new
@ideas = Idea.find(params[:idea_id])
@comment = Comment.new
end
def create
@ideas = Idea.find(params[:idea_id])
@comment = @ideas.comments.build(comment_params)
# @comment.user = current_user
if @comment.save
redirect_to new_idea_comment_path (@ideas)
end
end
private
def comment_params
params.require(:comment).permit(:id, :body)
end
end
(комментарии) _form.html.erb
<%= form_for [@ideas, @comment] do |f| %>
<div class="container">
<div class="form-group">
<%= f.label :body %>
<%= f.text_area :body, class: "form-control" %>
</div>
<div class="form-group">
<%= f.submit "Submit comment", class: "btn btn-primary" %>
<%= link_to "Go back?", root_path %>
</div>
</div>
<% end %>
(комментарии) edit.html.erb
<%= render "form" %>
(комментарии) new.html.erb
<%= render "form" %>
<% for comment in @ideas.comments %>
<div class="container">
<%= comment.body %>
<% if user_signed_in? && current_user.has_liked?(comment) %>
<% like = current_user.likes.find_by_comment_id(comment.id) %>
<%= link_to "unlike", comment_likes_path(comment, like), class: "btn btn-danger like ", method: :delete %>
<% else %>
<%= link_to "like", comment_like_path(comment), class: "btn btn-info like ", method: :post %>
<% end %>
<br>
<br>
</div>
<%end%>
likes_controller.rb
class LikesController < ApplicationController
def create
comment = Comment.find params[:comment_id]
like = comment.likes.new
like.user = current_user
if like.save
redirect_to new_idea_comment_path(comment.idea), notice: "Liked!"
else
redirect_to new_idea_comment_path(comment.idea), alert: "Already liked!"
end
end
def destroy
comment = Comment.find params[:comment_id]
like = comment.likes.find params[:id]
if like.destroy
redirect_to new_idea_comment_path(comment.idea), notice: "Unliked!"
else
redirect_to new_idea_comment_path(comment.idea), alert: "Can't unlike!"
end
end
end
comment.rb
class Comment < ActiveRecord::Base
belongs_to :idea
belongs_to :user
has_many :likes, dependent: :nullify
has_many :users, through: :likes
end
like.rb
class Like < ActiveRecord::Base
belongs_to :user
belongs_to :comment
end
user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
маршруты:
Rails.application.routes.draw do
devise_for :users
root "ideas#index"
resources :ideas do
resources :comments
end
resources :comments, only: [] do
resources :likes
end
Можете ли вы предоставить информацию о номере строки и файла, в котором исключение наступающем? – Nishu
У вас нет '.has_liked?' Метода экземпляра в классе 'User', почему он работает? –
@Nishu Я обновил свое оригинальное сообщение, чтобы включить его. – roguerat