我在我的项目中使用 gem breadcrumbs on rails 和 devise。
如果您还没有使用设计制作用户模型,请先制作:
rails g devise User
rake db:migrate
rails generate devise:views users
我的 registration_controller.rb 看起来像这样:
# app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
add_breadcrumb "home", :root_path
add_breadcrumb "contact", :contacts_path
end
我改变了路线:
devise_for :users, :controllers => { registrations: 'registrations' }
在 application.html.erb 布局中,我添加了面包屑(就在 上方)
<%= render_breadcrumbs %>
我刚刚测试过,从截图中可以看出效果。
已编辑:
如果您想在 Devise gem 的其他页面添加面包屑,例如忘记密码页面,您可以制作新的控制器:
# app/controllers/passwords_controller.rb
class PasswordsController < Devise::PasswordsController
add_breadcrumb "home", :root_path
add_breadcrumb "contact", :contacts_path
end
并更新您的路线:
devise_for :users, controllers: {
registrations: 'registrations',
passwords: 'passwords'
}
如果它适合你,请告诉我。