Ruby 在 Rails 上:使用 Devise 获取路线
Ruby on Rails: Getting a route with Devise
TL;DR 我怎样才能做到这一点总是 return 在自定义控制器中使用设计用户模型 ID 的路由(即重定向到 localhost:3000/profiles/504026426
)蒙古人?
我有一个项目有一个社交配置文件控制器,该控制器 returns 的用户 ID 与 Devise 但它总是会抱怨 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/profiles/504026426)。如果要存储用户以在索引视图中可用,请使用会话和当前用户帮助器方法。参见 here。
如果您希望 GET 方法与 'params[:id]' 一起使用,它看起来像:
get 'profile/:id' => 'profiles#show'
确保它与您控制器中的显示功能相匹配
def show
# this would work with: localhost:3000/profiles/504026426
@user = User.find(params[:id])
end
TL;DR 我怎样才能做到这一点总是 return 在自定义控制器中使用设计用户模型 ID 的路由(即重定向到 localhost:3000/profiles/504026426
)蒙古人?
我有一个项目有一个社交配置文件控制器,该控制器 returns 的用户 ID 与 Devise 但它总是会抱怨 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/profiles/504026426)。如果要存储用户以在索引视图中可用,请使用会话和当前用户帮助器方法。参见 here。
如果您希望 GET 方法与 'params[:id]' 一起使用,它看起来像:
get 'profile/:id' => 'profiles#show'
确保它与您控制器中的显示功能相匹配
def show
# this would work with: localhost:3000/profiles/504026426
@user = User.find(params[:id])
end