我在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不起作用 不知道怎么解决这个问题?感谢
答案 0 :(得分:3)
当你想要使用时......
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)
答案 1 :(得分:3)
更新到5.2.1解决它。
gem 'friendly_id', '~> 5.2.1'
答案 2 :(得分:1)
我相信你要做的就是用户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
扩展了模型。如果有效,请告诉我。