2015-10-07 157 views
1

用户配置文件我在做家居控制器的动作,我想,当我的个人资料链接用户点击只显示www.abc.com/profile路由问题在Ruby on Rails的

对于它,我做

get '/profile' => 'home#profile' 

但现在的问题是,有必要与that.If我的格式发送它一起发送用户ID,然后在URL成为

www.abc.com/profile.4 

我想要的网址仍然是“/ profile文件”和用户ID还发送

+0

您是否已经设置登录功能? –

+1

是通过设计 –

+0

请粘贴你的控制器在这里。 –

回答

1

如果你想让url保持/profile那么你将不得不依赖你的登录实现。我没有用过自己的设计,但根据文档,它提供了一个current_user方法用于控制器。所以,你会HomeController#profile这个样子:

def profile 
    @user = current_user 
    render :profile 
end 

这样,你不需要客户端的ID发送给您,因为(据我所知),这应该是登录用户的个人资料页,你应该已经掌握了通过设计的信息。

否则,你需要通过定义,像这样的路线,让ID才能通过的路径:

get '/profile/:id' => "home#profile" 
+0

我这样做得到'/ profile /:id'=>“家庭#个人资料”,但是当我做耙路线没有前缀来这个。我使用哪个前缀? –

+1

你可以像'as'一样规定一个前缀:get“/ profile /:id”=>“home#profile”,如:“profile” –

2

这是因为你设置它为member路线(它希望通过id为控制器)。你需要用collection路线来取代它,如果打算窝吧:

#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? %> 

如果您想让它让你可以不用嵌套访问profile,你的路线将工作:

#config/routes.rb 
get "profile", to: "home#profile", as: :profile 

<%= link_to "Profile", profile_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 

这使我们能够使用的功能如下(我们还可以使用色器件):

#config/routes.rb 
resource :profile, path: "profile", controller: :users, path_names: { edit: "" }, only: [:edit, :update] #-> domain.com/profile 
+0

当我这样做时,没有任何显示与耙路相关 –

+0

显示路线文件和耙路径。我不会为你做你的工作,努力工作。 –

+0

我不认为'路径:“配置文件”'部分是必要的......是吗? –

0
在色器件宝石

他们提到了CURRENT_USER模型,以便使用

current_user.id 

要找到用户ID,以便在您的个人资料操作中这样做,并传递当前用户的用户ID,以便您可以轻松获取他的详细信息

+0

问题是如何保持URL为'/ profile' –

+0

@RichPeck谢谢你的回复是的,我误解了这个问题。 –