不,我不允許看其他的個人資料
我們有這樣的設置:
這給我們致電users
控制器與所述URL的edit
和update
行動的能力: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
添加用戶名字段?你是否允許用戶看到彼此的個人資料? –
但是,我們不需要用戶的姓名日期;任何其他建議。 –
不,我不允許看到其他人的個人資料。 –