【问题标题】:How to set a conditional based on multiple models?如何设置基于多个模型的条件?
【发布时间】: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


【解决方案1】:

首先,根据 MVC,您不应该在视图中进行 SQL 查询:

&lt;% if Comment.find_by(notification.comment_id) == Habit.find(params[:habit_id]) %&gt; 不好的做法!

您应该在控制器中进行查询,然后在视图中使用控制器的实例变量(带有@的变量)。

其次,params[:habit_id] 仅在您的 url 有参数“habit_id”时才有效:?habit_id=[id] 但由于您的视图不应该进行任何查询,因此您应该先更改它。

【讨论】:

  • 谢谢!我只是把它放在问题的视图中。不会再这样做了:) 对于你的第二点,你能详细说明我做错了什么吗?我应该以某种方式在带有:habit 的通知中添加参数吗?
  • 访问该视图的 url 是什么?
  • 我还没有完全弄清楚。我想先让条件工作,但现在我把habit_path(notification)valuation_path(notification) 导致前者:0.0.0.0:3000/habits/9 但这是错误的,因为他们不习惯使用该ID。就像我说的,我最终需要弄清楚如何解决这个问题:/
猜你喜欢
  • 1970-01-01
  • 2018-05-25
  • 2016-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-30
  • 2019-08-15
  • 1970-01-01
相关资源
最近更新 更多