2015-10-07 76 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謝謝你的回覆是的,我誤解了這個問題。 –