如何在使用friendly_id

时间:2016-05-10 23:04:07

标签: ruby-on-rails ruby friendly-id

我目前的帖子模型使用friendly_id将Post.id替换为Post.pidfriendly_id :pid, :use => :scoped, :scope => :id

如何获取帖子的实际ID而不是slug,以便在回复保存时将Post.id而不是Post.slug保存到Reply.post_id

我需要这样做,因为由于post.pid不唯一而在其他主板上创建的帖子会显示回复

class RepliesController < ApplicationController
  def create
    @board = Board.friendly.find(params[:board_id])
    @post = Post.friendly.find(params[:post_id])
    @reply = @post.replies.create(reply_params)
    @post.touch
    redirect_to @board
  end
  private
    def reply_params
      params.require(:reply).permit(:name, :email, :subject, :comment, :reply_file)
    end
end

1 个答案:

答案 0 :(得分:1)

尝试通过董事会确定您的Post.find

@post = @board.posts.friendly.find params[:post_id]