【问题标题】:acts_as_followers and friendly_id gems not finding idact_as_followers 和 friendly_id gem 找不到 id
【发布时间】:2017-05-30 12:01:43
【问题描述】:

我正在使用 acts_as_follower gem 和 friendly_id gem。

我设置了acts_as_follower,一切正常,我可以根据需要关注Profiles。但现在我添加了friendly_id gem 以将配置文件网址显示为profile/myname 而不是profile/1

但现在acts_as_follower gem 不起作用,它找不到要关注的个人资料 ID:

这是我现在正在尝试的设置,但这仍然不起作用。

  def follow
    @profile = Profile.find(params[:profile_id])
    current_user.follow(@profile)
    redirect_to :back
  end

  def unfollow
    @profile = Profile.find(params[:profile_id])
    current_user.stop_following(@profile)
    redirect_to :back
  end

之前:

@profile = Profile.find(params[:id])

我得到的错误是:

Couldn't find Profile with 'id'=

正在传递的参数是:

{"id"=>"gurmukh-singh"} 

它现在寻找的 id 是友好的 url name

另外,新的friendly_id 版本要求我找到这样的个人资料:

def set_story
  @profile = Profile.friendly.find(params[:id])
end

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 friendly-id acts-as-follower


    【解决方案1】:

    在您的控制器中,您需要将其更改为

      def follow
        @profile = Profile.friendly.find(params[:id])
        current_user.follow(@profile)
        redirect_to :back
      end
    
      def unfollow
        @profile = Profile.friendly.find(params[:id])
        current_user.stop_following(@profile)
        redirect_to :back
      end
    

    那么这应该可以工作

    【讨论】:

    • 仍然收到此错误Couldn't find Profile without an ID 并且正在发送的参数是{"id"=>"gurmukh-singh"}。 id 应该是 1 之类的东西,不确定为什么要传递名称,因为它试图找到 (params[:profile_id])
    • 它不是一个,因为友好的 ID 把它变成了一个蛞蝓
    • 它试图找到id,但它传递的参数是slug,但找不到它,因为没有id,它存储为slugged名称
    • 它有效,但必须做一个小改动,iv 编辑了答案
    猜你喜欢
    • 2014-03-19
    • 2015-11-08
    • 2013-03-26
    • 2015-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-18
    相关资源
    最近更新 更多