2015-11-12 78 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 %> 
相关问题