使用friendly_id时如何find_by_id(用于多态关系)

杰克

我如何允许 find_by_id 工作,以便我的评论可以与我的帖子一起使用friendly_id有没有其他方法可以做到这一点?

我安装了一个名为 Ruby gemfriendly_id并使用它slugs在我的博客文章中创建这些博客文章通过polymorphic关系发表评论我有这些相互矛盾的方法,我认为这是我的评论不起作用并引发错误的原因:

undefined method 'comments' for nil:NilClass

指着@comment = @commentable.comments.new comment_params创建方法意见控制器

在我的评论模型中:

class Comment < ApplicationRecord
  belongs_to :commentable, polymorphic: true
  has_many :comments, as: :commentable, dependent: :destroy
end

在我的评论控制器中:

class CommentsController < ApplicationController
  before_action :find_commentable

  def new
    @comment = Comment.new
  end

  def create
    @comment = @commentable.comments.new comment_params
    if @comment.save
      flash[:success] = "Thanks for sharing your thoughts!"
      redirect_back fallback_location: root_path
    else
      flash[:danger] = "There was an error posting your comment!"
      redirect_back fallback_location: root_path
    end
  end

  private

  def comment_params
    params.require(:comment).permit(:body, :email, :name)
  end

  def find_commentable
    @commentable = Comment.find_by_id(params[:comment_id]) if params[:comment_id]
    @commentable = Post.find_by_id(params[:post_id]) if params[:post_id]
  end
end

在我的帖子模型中: has_many :comments, as: :commentable

我的路线:

resources :comments do
  resources :comments
end

我的服务器日志:

app/controllers/comments_controller.rb:9:in `create'
Started POST "/posts/top-5-audio-industry-blogs/comments" for 127.0.0.1 at 2018-06-30 10:35:09 -0300
Processing by CommentsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"[token]", "comment"=>{"body"=>"Hey, thanks for checking out the article! Any questions? Just ask me here and I'll be happy to help.", "name"=>"name", "email"=>"email", "nickname"=>""}, "commit"=>"Post Comment", "post_id"=>"top-5-audio-industry-blogs"}
Post Load (0.0ms)  SELECT  "posts".* FROM "posts" WHERE "posts"."id" = $1 ORDER BY "posts"."created_at" DESC LIMIT $2  [["id", 0], ["LIMIT", 1]]
Completed 500 Internal Server Error in 27ms (ActiveRecord: 6.0ms)



NoMethodError (undefined method `comment' for nil:NilClass):

这是我能想到的唯一导致此错误的原因,因为它在没有 slug 的情况下工作。提前感谢您的任何帮助!

贾格迪普·辛格

根据docs,您需要稍微不同地查询CommentPost模型,因为帖子有slugs而评论没有。见下文:

def find_commentable
  @commentable = if params[:comment_id]
                   Comment.find_by_id(params[:comment_id])
                 elsif params[:post_id]
                   Post.friendly.find(params[:post_id])
                 end

  # You should also handle the case if `@commentable` is `nil` after above.
  redirect_to somewhere, error: 'Post/Comment not found' unless @commentable
end

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Rails:friendly_id slug 关系

尝试更新多个记录时,Rails在使用friendly_id时找不到ID为ID的记录

在Rails friendly_id中,如何为子弹使用多个属性?

在friendly_id gem中使用之前如何更改slug?

尽管被重写,但使用UUID的Friendly_id gem

避免使用friendly_id gem的RecordNotFound异常

RoR:用户唯一的friendly_id

current_user的friendly_id gem

在网址中使用Slug和id和Friendly_Id宝石

通过friendly_id而不是id查找项目

Rails:如何将参数从friendly_id URL传递给控制器?

使用用户名列作为 slug (friendly_id)?

Friendly_id和全球化-如果缺少翻译,则对子词使用默认翻译

在friendly_id的slug_candidates中使用关联的字段名称

Rails:将friendly_id/slug 与关联模型中的列一起使用

使用friendly_id更新属性后无法更改URL路径

rails4 add friendly_id具有错误的未定义方法“ slug”,用于

尝试复制friendly_id将强者变成a的行为

Rails 4 + Friendly_id:URL未显示任何错误

未定义的方法'slug_limit ='for friendly_id

将@添加到friendly_id slug Ruby on Rails

friendly_id slug不是在create上生成的

Friendly_id无法正常工作,版本5.3导轨6

Friendly_id 和两个 slug 从 root 开始

Friendly_Id:id很低,但是url仍显示对象id属性(?id =)

如何将旧网址重定向到 rails 应用程序中新的friendly_id 生成的网址?

使用Record.friendly.find参数阻止ID

未定义的方法错误 'find(), find_by(), find_by_id(), where()' Ruby on Rails

Rails4 Friendly_id唯一子弹格式