如何让 friendly_id gem 开始使用 Rails 5.0.0.rc1?

How to get friendly_id gem get working with Rails 5.0.0.rc1?

我在 rails 应用程序中使用 friendly_id。我在 gemfile 中有以下内容。

gem 'friendly_id', '~> 5.1'

每当我访问用户个人资料时,我都会收到 404 错误。

suai@rails-tutorial:~/workspace/iya (master) $ rails server -b $IP -p $PORT
=> Booting WEBrick
=> Rails 5.0.0.rc1 application starting in development on http://0.0.0.0:8080
=> Run `rails server -h` for more startup options
[2016-06-07 05:54:21] INFO  WEBrick 1.3.1
[2016-06-07 05:54:21] INFO  ruby 2.3.1 (2016-04-26) [x86_64-linux]
[2016-06-07 05:54:21] INFO  WEBrick::HTTPServer#start: pid=74846 port=8080
Started GET "/users/testtest" for 98.5.56.144 at 2016-06-07 05:54:25 +0000
Cannot render console from 98.5.56.144! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
  ActiveRecord::SchemaMigration Load (0.2ms)  SELECT "schema_migrations".* FROM "schema_migrations"
Processing by UsersController#show as HTML
  Parameters: {"id"=>"testtest"}
  User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 0], ["LIMIT", 1]]
  Rendering public/404.html within layouts/application
  Rendered public/404.html within layouts/application (8.5ms)
  Rendered layouts/_shim.html.erb (0.8ms)
  Rendered layouts/_header.html.erb (2.3ms)
  Rendered layouts/_footer.html.erb (2.2ms)
Completed 404 Not Found in 924ms (Views: 848.5ms | ActiveRecord: 2.1ms)

用户控制器,显示方法

  def show
    @user = User.find(params[:id])
    @microposts = @user.microposts.paginate(page: params[:page])
    respond_to do |format|
      format.html
      format.js    
    end      
  end

User.rb

  extend FriendlyId
  friendly_id :name, use: [:slugged, :finders]   

我认为 friendly_id 不起作用。
知道如何解决这个问题吗?谢谢

我相信您要做的是用户 Model.friendly.find 而不是 find

def show
  @user = User.friendly.find(params[:id])
  @microposts = @user.microposts.paginate(page: params[:page])
  respond_to do |format|
    format.html
    format.js    
  end      
end

我希望您已经使用 FriendlyId 扩展了您的模型。让我知道它是否有效。

当您想使用...

extend FriendlyId
friendly_id :name, use: [:slugged, :finders] 

...你需要安装master分支(friendly_id 5.2.0.beta.1)

gem 'friendly_id', github: 'norman/friendly_id'

如果这样做,则不必更换控制器(Model.friendly.find)

更新到5.2.1解决。

gem 'friendly_id', '~> 5.2.1'