【发布时间】:2019-01-27 23:15:13
【问题描述】:
有人可以提供有关如何使用friendly_id 发帖的提示吗?
在设置了一个简单的评论系统后,没问题,但是使用friednly_id,我尝试在日志中发布评论轨
Completed 422 Unprocessable Entity in 241ms
似乎是因为没有将正确的 id 传递给参数
Parameters: {"comment"=>{"commentable_id"=>"dde", "comment"=>"swsww", "commentable_type"=>"Post", "parent_id"=>"", "post_id"=>"dde"}, "post_id"=>"dde"}
更多相关信息:
我尝试过直接使用帖子而不是资源,但还是一样
set_commentable
@commentable = if params[:comment_id]
Comment.find_by_id(params[:comment_id])
elsif params[:post_id]
Post.friendly.find(params[:post_id])
end
end
def set_comment
@comment = @commentable.comments.friendly.find(params[:id])
rescue StandardError => e
logger.error "#{e.class.name} : #{e.message}"
@comment = @commentable.comments.build
@comment.errors.add(:base, :recordnotfound, message: "That record doesn't exist. Maybe, it is already destroyed.")
end
def set_commentable
resource, id = request.path.split('/')[1, 2]
@commentable = resource.singularize.classify.constantize.friendly.find(id)
end
def set_post
@post = Post.friendly.find(params[:post_id] || params[:id])
end
预期的结果将是发表评论没有任何错误
【问题讨论】:
-
如果您添加有关错误堆栈跟踪的更多信息会更好。
-
@SebastianPalma,谢谢我添加了错误图片
-
在
rails console试试吧。
标签: javascript ruby-on-rails ruby reactjs