2016-03-07 45 views
0

我希望爲用戶創建,簽署了 所以假設用戶已經註冊,個人資料頁他的個人資料鏈接應該
Ruby on Rails的我的資料

website.com/profile/username 

,並在 views/profile/index.html.erb 每個用戶應該看到自己的個人資料,並與的form_for編輯我猜

到目前爲止,我profiles_controller.rbprofile.rb模型和resources :profiles我的routes.rb

這樣做的最佳方法是什麼?

回答

0

如果你想通過用戶名得到,那麼你需要使用slu。。以下示例適用於id版本。

檔案控制器

def show 
    @profile = Profile.find(params[:id]) 
end 

路線:

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

:slug版本,網址將是這樣的:http://localhost:3000/profiles/**username**

路線:

resources :profiles, param: :slug 

profiles_controller

def show 
end 

private 

def set_profile 
    @profile = Profile.find_by_username(params[:slug]) 
end 

或者你可以使用寶石本https://github.com/norman/friendly_id

+0

我想你也應該寫的觀點的例子嗎? – user4571629

+0

@ user4571629你想讓我製作整個頁面嗎?這個答案是針對你的問題的。 – 7urkm3n

+0

@ user4571629另外,如果想要編輯twitter等選項,那麼您可以通過ajaxing獲得編輯表單。 – 7urkm3n