Ruby on rails rubyonrails中的路由问题
对于用户配置文件,我在home controller中执行一个操作,我希望当用户单击我的配置文件链接时,只显示www.abc.com/profile 为此,我愿意Ruby on rails rubyonrails中的路由问题,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,对于用户配置文件,我在home controller中执行一个操作,我希望当用户单击我的配置文件链接时,只显示www.abc.com/profile 为此,我愿意 get '/profile' => 'home#profile' 但现在的问题是,需要同时发送用户id。若我以这种格式发送,那个么url就变成了 www.abc.com/profile.4 我希望url保持“/profile”,并且用户Id也发送如果您希望url保持/profile,那么您必须依赖您的登录实现。我自己没有使用
get '/profile' => 'home#profile'
但现在的问题是,需要同时发送用户id。若我以这种格式发送,那个么url就变成了
www.abc.com/profile.4
我希望url保持“/profile”,并且用户Id也发送如果您希望url保持
/profile
,那么您必须依赖您的登录实现。我自己没有使用Desive,但根据文档,它为您提供了一种在控制器中使用的当前用户
方法。因此,您的HomeController#配置文件
将如下所示:
def profile
@user = current_user
render :profile
end
这样,您就不需要客户端向您发送id,因为(据我所知)这应该是登录用户的配置文件页面,您应该已经通过Desive获得了有关该页面的信息
否则,您需要通过如下定义路由来允许id通过路径:
get '/profile/:id' => "home#profile"
这是因为您正在将其设置为路由(它希望传递控制器的
id
)。如果要嵌套管线,则需要将其替换为管线:
#config/routes.rb
devise_for :users ... do
get :profile, to: "home#profile", on: :collection, as: :profile #-> url.com/users/profile
end
然后,您可以按如下方式访问配置文件:
<%= link_to "Profile", users_profiles_path, if user_signed_in? %>
控制器 我们有一个类似的设置,让我解释一下控制器的设置
#app/controllers/users_controller.rb
class UsersController < ApplicationController
before_action :authenticate_user!
def edit
@user = current_user
end
def update
@user = current_user
...
end
end
在Desive gem中,他们提到了当前的用户模型,以便使用
current_user.id
要查找用户id以便在您的配置文件操作中执行此操作,并传递当前用户的用户id以便您可以轻松获取其详细信息您是否已经设置了登录功能?请将您的控制器粘贴到此处。我通过这种方式获得“/profile/:id”=>“home#profile”,但如果我这样做,则没有前缀。我使用哪一个前缀?您可以将前缀设置为“as”,例如:get“/profile/:id”=>“home#profile”,设置为:“profile”,当我这样做时,rake routes show routes文件和rake routes中没有显示与之相关的内容。我不会为你做你的工作,努力吧。我不认为
路径:“profile”
部分是必要的。。。是吗?不在下面的例子中。事实上,我应该改变这一点,因为我是从我们自己的作品中提取出来的code@RichPeck谢谢你的回答是的,我误解了这个问题。
#config/routes.rb
resource :profile, path: "profile", controller: :users, path_names: { edit: "" }, only: [:edit, :update] #-> domain.com/profile
current_user.id