我使用Devise gem在功能中进行用户注册。我只通过用户注册表单获得email id and password
的值。
如果我转到用户的展示页面,则该网址不能很好地描述其内容。它会在网址中显示primary id's
值,如下所示
http://localhost:3000/users/17
然后,我决定使用friendly_id
Gem。
因此,我没有在注册表单中获得用户的名称。现在,我没有在网址中使用任何其他值。
在这种情况下我该怎么做。请提出一些想法。我该如何处理这个问题!...
答案 0 :(得分:3)
No, I don't allow to see other's profile
We have this setup:
This gives us the ability to call the edit
and update
actions of the users
controller with the url: url.com/profile
You'd be able to set it up as follows:
#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 %>
This sounds like what you need.
If you wanted to set up friendly_id
without the comparative username
etc, we use a Profile
model which allows you to add a username if you wish:
#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
We then manage to look up the profile
with a little bit of a hack:
#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 :(得分:1)
使其成为一种独特的资源
resource :user
然后它就会转到/user
在您的表单中,您需要明确显示网址,因为铁路无法推断它是一种独特的资源
<%= form_for @user, url: user_path %>