繁体   English   中英

Ruby on Rails:通过Devise获得路线

[英]Ruby on Rails: Getting a route with Devise

TL; DR我该如何实现始终在带有Mongoid的自定义控制器中将具有Devise用户模型ID的路由返回(即,重定向到localhost:3000/profiles/504026426 )?

我有一个带有社交配置文件控制器的项目,该项目使用Devise返回了一个用户ID,但它总是会抱怨Mongoid需要有效的ID号才能使用此路由: localhost:3000/profiles/

# Let's say I want to return a route with id (Devise) without having
# Rails to complain that Mongoid needs to a id of some number.

# So, I have controller containing profiles in the files.
# It goes like..
class ProfilesController < ApplicationController
  before_filter :authenticate_user!
    # GET /profile/
    def index
      @user = User.find(params[:id])
    end
    # .. Snipped for brevity.
end

这是正确的方法吗?

首先,您试图通过其查找用户的'params [:id]'在索引视图中不起作用,因为您没有通过url传递任何参数('profiles /:id'或:localhost :3000 /简档/ 504026426)。 如果要存储可在索引视图中使用的用户,请使用会话和当前的用户帮助器方法。 这里

如果您希望GET方法与'params [:id]'一起使用,它将看起来像:

get 'profile/:id' => 'profiles#show'

确保它与控制器中的显示功能匹配

def show
  # this would work with: localhost:3000/profiles/504026426
     @user = User.find(params[:id])
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM