通知/索引有<%= render partial: "notifications/notification", collection: @notifications %>
,其中包含:
<% if Comment.find_by(notification.comment_id) == @habit %>
<%= link_to "your habit", habit_path(notification) %>
<% else %>
<%= link_to "your value", valuation_path(notification) %>
<% end %>
显示第1行的错误:
找不到具有&#39; id&#39; =
的习惯
即使控制台显示:
pry(main)> Comment.find(1)
Comment Load (0.3ms) SELECT "comments".* FROM "comments" WHERE "comments"."id" = ? LIMIT 1 [["id", 1]]
=> #<Comment:0x007f86201267b0
id: 1,
content: "test",
goal_id: nil,
habit_id: 1,
valuation_id: nil,
quantified_id: nil,
commentable_id: nil,
commentable_type: nil,
user_id: 1,
created_at: Thu, 04 Jun 2015 00:07:22 UTC +00:00,
updated_at: Thu, 04 Jun 2015 04:04:29 UTC +00:00,
我们如何使用条件,以便每个通知都指向正确的路径?
notifications_controller
def index
@habit = Habit.find(params[:habit_id])
@notifications = current_user.notifications
@notifications.each do |notification|
notification.update_attribute(:read, true)
end
end
通知基于用户是否评论习惯或价值:
comment.rb
class Comment < ActiveRecord::Base
after_save :create_notification
has_many :notifications
belongs_to :commentable, polymorphic: true
belongs_to :user
validates :user, presence: true
private
def create_notification
Notification.create(
user_id: self.user_id,
comment_id: self.id,
read: false
)
end
end
我遵循了此通知教程,但它仅使用一个模型:http://evanamccullough.com/2014/11/ruby-on-rails-simple-notifications-system-tutorial/
comments_controller
class CommentsController < ApplicationController
before_action :set_commentable, only: [:index, :new, :create]
before_action :set_comment, only: [:edit, :update, :destroy, :like]
def index
@comments = @commentable.comments
end
def new
@comment = @commentable.comments.new
end
def create
@comment = @commentable.comments.new(comment_params)
if @comment.save
redirect_to @commentable, notice: "Comment created."
else
render :new
end
end
def edit
end
def update
if @comment.update_attributes(comment_params)
redirect_to :back, notice: "Comment was updated."
else
render :edit
end
end
def destroy
@comment.destroy
redirect_to @comment.commentable, notice: "Comment destroyed."
end
def like
@comment_like = current_user.comment_likes.build(comment: @comment)
if @comment_like.save
@comment.increment!(:likes)
flash[:success] = 'Thanks for liking!'
else
flash[:error] = 'Too many likes'
end
redirect_to(:back)
end
private
def set_commentable
@commentable = find_commentable
end
def set_comment
@comment = current_user.comments.find(params[:id])
end
# add more commentable models here
def find_commentable
if params[:goal_id]
Goal.find(params[:goal_id])
elsif params[:habit_id]
Habit.find(params[:habit_id])
elsif params[:valuation_id]
Valuation.find(params[:valuation_id])
elsif params[:quantified_id]
Quantified.find(params[:quantified_id])
end
end
def comment_params
params[:comment][:user_id] = current_user.id
params.require(:comment).permit(:content, :commentable, :user_id, :like)
end
end
答案 0 :(得分:1)
首先,根据MVC,您不应该在视图中进行SQL查询:
<% if Comment.find_by(notification.comment_id) == Habit.find(params[:habit_id]) %>
不好的做法!
您应该在控制器中进行查询,然后在视图中使用控制器的实例变量(带@的变量)。
其次,params[:habit_id]
仅在您的网址具有以下内容的参数'habit_id'时才有效:?habit_id=[id]
但由于您的视图不应该进行任何查询,因此您应该先更改它。