ruby-on-rails - 尝试让经过身份验证的用户首先登陆他们的个人资料页面而不是 home#index - 使用 Devise

标签 ruby-on-rails ruby devise

我的思绪运转缓慢……这是一个简单的问题……我知道。请原谅我的菜鸟...

我想将经过身份验证的用户定向到该用户的个人资料页面。

注意:我正在使用 Devise。

注意 2:您可以在此处查看整个 routes.rb 文件....pastie.org/6142383

当我执行“rake routes”时,我将其作为路线信息....

profile GET        /profiles/:id(.:format)                       profiles#show

所以在我的 routes.rb 文件中,我试过这个...

authenticated :user do
  root :to => 'profiles#show'
end

但是我收到一个错误“app/controllers/profiles_controller.rb line 17”,这是...

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

所以我需要以某种方式在我的 routes.rb 中指示 params[:id] 但我很困惑如何...

authenticated :user do
  root :to => '?'  # how to land user on www.website.com/profile/1
end 

感谢您的宝贵时间...

更新:

我正在使用 gem '更好的错误'(在这里找到关于这个 gem 的信息)http://selfless-singleton.rickwinfrey.com/2012/12/13/better-errors/错误显示....

ActiveRecord::RecordNotFound at /
Couldn't find Profile without an ID

ProfileControllers#show
app/controllers/profiles_controller.rb, line 17

然后向我显示确切的行...

12   end
13 
14   # GET /profiles/1
15   # GET /profiles/1.json
16   def show
17     @profile = Profile.find(params[:id])
18 
19     respond_to do |format|
20       format.html # show.html.erb
21       format.json { render json: @profile }
22     end

最佳答案

听起来你想要一个 Singular Resource除了您的其他个人资料。 Devise 中的以下作品:

resources :users

authenticate(:user) do
  get '/profile', to: 'users#show'
end

然后在您的 users_controller.rb 中:

def show
  if params[:id]
      @user = User.find(params[:id])
  else
    # Show the currently logged in user
    @user = current_user
  end
  ....
end

要自定义用户在成功登录后登陆的位置,请在您的 ApplicationController 中添加/自定义此方法:

def after_sign_in_path_for(resource)
 current_user_path
end

其他有用的指南:https://github.com/plataformatec/devise/wiki/_pages

关于ruby-on-rails - 尝试让经过身份验证的用户首先登陆他们的个人资料页面而不是 home#index - 使用 Devise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14841054/

相关文章:

ruby-on-rails - ruby 动态方法传递一个值

ruby - 使用带有 rails : current_password being sanitized? 的设计

ruby-on-rails - 载波上传图片

ruby-on-rails - 在 Rails 中处理带有 "action"参数的 POST

ruby-on-rails - 参数缺失或值为空

ruby - 根据用户输入循环创建

ruby-on-rails - Devise 的匿名用户?

ruby-on-rails - 设计中的嵌套资源

ruby-on-rails - Rails 服务器上的 NoMethodError

html - 如何链接到 html.erb 页面?