2015-11-12 27 views
1

我有使用Devise gem的功能註冊用戶。我只通過用戶註冊表單獲得email id and password的價值。如何在用戶名稱丟失時修復用戶個人資料網址?

如果我去了一個用戶的顯示頁面,那麼這個url對它的內容並不是非常具有描述性。這表明primary id's值的網址,如下所示

http://localhost:3000/users/17 

比,我決定用覆蓋寶石friendly_id默認行爲。

因此,我沒有在註冊表單中獲取用戶的名稱。現在,我沒有任何其他價值在網址中使用。

在這種情況下我應該做什麼。請提出一些想法。如何處理這個問題!...

+0

添加用戶名字段?你是否允許用戶看到彼此的個人資料? –

+0

但是,我們不需要用戶的姓名日期;任何其他建議。 –

+0

不,我不允許看到其他人的個人資料。 –

回答

3

不,我不允許看其他的個人資料

我們有這樣的設置:

enter image description here

這給我們致電users控制器與所述URL的editupdate行動的能力:url.com/profile

,您將可以設置如下:

#app/controllers/users_controller.rb 
class UsersController < ApplicationController 
    def edit 
     #use current_user 
    end 

    def update 
     redirect_to profile_path if current_user.update profile_params 
    end 
end 

#app/views/users/edit.html.erb 
<%= form_for current_user do |f| %> 
    <%= f.text_field ....... %> 
    <%= f.submit %> 
<% end %> 

這聽起來像你所需要的。


如果你想建立friendly_id沒有比較username等,我們使用了Profile模型,它允許您根據需要添加用戶名:

#app/models/user.rb 
class User < ActiveRecord::Base 
    has_one :profile 
    before_create :build_profile 
    delegate :name, to: :profile 
end 

#app/models/profile.rb 
class Profile < ActiveRecord::Base 
    belongs_to :user 

    extend FriendlyId 
    friendly_id :name 
end 

然後我們管理來查找profile有一點點黑客:

#app/controllers/users_controller.rb 
class UsersController < ApplicationController 
    def show 
     @user = Profile.find(params[:id]).user #-> friendly_id looks up the :name column in users 
    end 
end 
1

使它成爲一個單一的資源

resource :user 

然後它會只是爲了/user

在您的形式路線,你需要做出明確的網址鐵軌將無法推斷出這是一個奇異的資源

<%= form_for @user, url: user_path %> 
相關問題