【发布时间】:2015-06-04 22:49:23
【问题描述】:
通知/索引有<%= 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 %>
显示第一行的错误:
找不到“id”= 的习惯
即使控制台显示:
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/
cmets_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
【问题讨论】:
-
你的参数是什么?
-
没有@test 我做的教程里没有。那是现在需要的东西吗?
-
所以在索引方法中你的@habit 必须是 nil...对....?
-
是的,它给出了错误
ActiveRecord::RecordNotFound (Couldn't find Habit with 'id'=): app/controllers/notifications_controller.rb:3:in 'index'和[["id", nil]] -
无论何时调用通知的索引方法,您都应该发送habit_id,您的错误将得到解决
标签: ruby-on-rails ruby model-view-controller notifications conditional