因此,我有一个应用程序,允许用户在注册时选择用户名-然而,当他们登录时,他们的用户名是空白的,而应该在注册时保存,并显示在他们的个人资料中。
以下是注册表格的快照:
<div class="home-page-m">
<div class="container">
<p class="banner-heading">Meet new people and make new friends</p>
<div class=" col-md-4 col-xs-12 sign-up-form">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Sign Up</h3>
</div>
<div class="panel-body">
<%= simple_form_for('user', :as => 'user', :url => registration_path('user')) do |f| %>
<form role="form">
<div class="form-group">
<label for="exampleInputEmail1"></label>
<%= f.text_field :username, class: 'form-control', id: 'Email1', placeholder: 'Username', required: true %>
</div>
<div class="form-group">
<label for="exampleInputEmail1"></label>
<%= f.text_field :first_name, class: 'form-control', id: 'Email1', placeholder: 'First Name', required: true %>
</div>
<div class="form-group">
<label for="exampleInputEmail1"></label>
<%= f.text_field :last_name, class: 'form-control', id: 'Email1', placeholder: 'Last Name', required: true %>
</div>
<div class="form-group">
<label for="exampleInputEmail1"></label>
<%= f.email_field :email, class: 'form-control', id: 'Email1', placeholder: 'Email', required: true %>
</div>
<div class="form-group">
<label for="exampleInputPassword1"></label>
<%= f.password_field :password, class: 'form-control', id: 'Password', placeholder: 'Password', required: true %>
</div>
<div class="form-group">
<label for="exampleInputEmail1"></label>
<%= f.text_field :zip, class: 'form-control', id: 'zip', placeholder: 'Zip', required: true %>
</div>
<div class="form-group">
<label for="Date" style="display:block; margin-bottom:5px; text-decoration: underline;">Date of Birth :</label>
<div class="row birth-date">
<%= f.date_select :birthday, {
:order => [:month, :day, :year],
:start_year => 1950,
:end_year => 2004} %>
</div>
<div class="clr"></div>
</div>
<div class="col-md-12 text-center sign-b">
<%= f.submit 'Create Account', class: 'btn btn-default creat-event' %>
<p>By clicking the button you agree to<br>
the terms and conditions</p>
</div>
</form>
<% end %>
</div>
</div>
</div>
</div>
</div>
以下是注册后用户名应显示在用户配置文件中的位置:
<div class="users_about_fullinfo">
<h3><%= current_user.name %> <small>(<%= current_user.username %>)</small></h3>
<p class="edit-about-text">
<%= best_in_place current_user, :blurb, activator: ".edit-blurb", nil: 'Click here to add a short blurb (maximum 140 characters)', inner_class: 'form-control', ok_button: 'Save', ok_button_class: 'btn btn-default btn-xs creat-event blurb-ok', cancel_button: 'Cancel', cancel_button_class: 'btn btn-default btn-xs cencel-event blurb-cancel' %>
<span class="edit-icon edit-blurb"></span>
</p>
</div>
<div class="clr"></div>
为什么用户名没有显示在那里?请记住,current_user.name正在显示,但没有显示用户名。。
任何帮助都会很棒,干杯!
更新:使用用户控制器
class UsersController < ApplicationController
before_filter :authenticate_user!
def index
@users = User.all
end
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
respond_to do |format|
if @user.update(params[:user])
format.html { redirect_to profile_path}
format.json { respond_with_bip(@user) }
format.js
else
format.html { render edit}
format.json { respond_with_bip(@user) }
format.js
end
end
end
def show
@user = User.find(params[:id]) || current_user
@questions_for_about = @user.questions.for_about.order('id asc')
@questions_for_personality = @user.questions.for_personality.order('id asc')
end
def edit_profile
@user = current_user
@questions_for_about = @user.questions.for_about.order('id asc')
@questions_for_personality = @user.questions.for_personality.order('id asc')
end
def edit_age
end
def update_age
current_user.update_attributes(birthday: params[:user][:birthday])
end
def refresh_notifcations
@new_messages = current_user.new_messages.count
end
# disconnect from social networks
def disconnect
social = params[:social]
current_user.disconnect(social)
redirect_to :back
end
def visitors
@visitors = current_user.recent_visitors.order('visited_at desc')
end
def toggle_hidden
@hidden_user_id = params[:hidden_user_id] # sent to js.erb
if params[:action_type] == 'hide'
current_user.hidden_users.find_or_create_by(hidden_user_id: @hidden_user_id)
elsif params[:action_type] == 'unhide'
current_user.hidden_users.where('hidden_user_id = ?', @hidden_user_id).first.destroy
end
end
def toggle_blocked
@blocked_user_id = params[:blocked_user_id] # sent to js.erb
if params[:action_type] == 'unblock'
current_user.blocked_users.where('blocked_user_id = ?', @blocked_user_id).first.destroy
elsif params[:action_type] == 'block'
current_user.blocked_users.find_or_create_by(blocked_user_id: @blocked_user_id)
end
end
def account_registration
@hidden_users = User.where(id: current_user.hidden_users.pluck(:hidden_user_id))
@blocked_users = User.where(id: current_user.blocked_users.pluck(:blocked_user_id))
@events = current_user.events
@activities = current_user.activities
end
def update_profile_completness
respond_to do |format|
format.js
end
end
def disable_account
current_user.disable_account
Devise.sign_out_all_scopes ? sign_out : sign_out(current_user)
redirect_to root_path, notice: 'Account was disabled. Sign in to enable it again.'
end
private
# Using a private method to encapsulate the permissible parameters
# is just a good pattern since you'll be able to reuse the same
# permit list between create and update. Also, you can specialize
# this method with per-user checking of permissible attributes.
end
和
注册控制器
class Users::RegistrationsController < Devise::RegistrationsController
before_filter :load_zip_codes, only: [:create]
# GET /resource/edit
def edit
render :edit
end
# POST /resource
def create
build_resource(sign_up_params)
if @zip_codes.include?(resource.zip)
if resource.save
if resource.active_for_authentication?
set_flash_message :notice, :signed_up if is_navigational_format?
sign_up(resource_name, resource)
respond_with resource, :location => after_sign_up_path_for(resource)
else
set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
expire_session_data_after_sign_in!
respond_with resource, :location => after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords resource
respond_with resource
end
else
TempEmail.create(email: params[:user][:email], first_name: params[:user][:first_name],
last_name: params[:user][:last_name], zip: params[:user][:zip])
notice = "Sorry #{params[:user][:first_name]}, we are not accepting sign-ups from outside of New York City Metro Area at the moment, however, we will notify you when we launch in your area!"
redirect_to root_path, notice: notice
end
end
def update
if params[resource_name][:password].blank?
params[resource_name].delete(:password)
params[resource_name].delete(:password_confirmation) if params[resource_name][:password_confirmation].blank?
end
# Override Devise to use update_attributes instead of update_with_password.
# This is the only change we make.
if resource.update_attributes(params[resource_name])
set_flash_message :notice, :updated
# Line below required if using Devise >= 1.2.0
sign_in resource_name, resource, :bypass => true
redirect_to after_update_path_for(resource)
else
clean_up_passwords(resource)
render_with_scope :edit
end
end
def account_registration
@user = User.find(current_user.id)
end
def load_zip_codes
@zip_codes = ['10453', '10457', '10460', '10458', '10467', '10468', '10451', '10452', '10456', '10454', '10455', '10459',
'10474', '10463', '10471', '10466', '10469', '10470', '10475', '10461', '10462', '10464', '10465', '10472', '10473', '11212',
'11213', '11216', '11233', '11238', '11209', '11214', '11228', '11204', '11218', '11219', '11230', '11234', '11236', '11239',
'11223', '11224', '11229', '11235', '11201', '11205', '11215', '11217', '11231', '11203', '11210', '11225', '11226', '11207',
'11208', '11211', '11222', '11220', '11232', '11206', '11221', '11237', '10026', '10027', '10030', '10037', '10039', '10001',
'10011', '10018', '10019', '10020', '10036', '10029', '10035', '10010', '10016', '10017', '10022', '10012', '10013', '10014',
'10004', '10005', '10006', '10007', '10038', '10280', '10002', '10003', '10009', '10021', '10028', '10044', '10128', '10023',
'10024', '10025', '10031', '10032', '10033', '10034', '10040', '11361', '11362', '11363', '11364', '11354', '11355', '11356',
'11357', '11358', '11359', '11360', '11365', '11366', '11367', '11412', '11423', '11432', '11433', '11434', '11435', '11436',
'11101', '11102', '11103', '11104', '11105', '11106', '11374', '11375', '11379', '11385', '11691', '11692', '11693', '11694',
'11695', '11697', '11004', '11005', '11411', '11413', '11422', '11426', '11427', '11428', '11429', '11414', '11415', '11416',
'11417', '11418', '11419', '11420', '11421', '11368', '11369', '11370', '11372', '11373', '11377', '11378', '10302', '10303',
'10310', '10306', '10307', '10308', '10309', '10312', '10301', '10304', '10305', '10314']
end
private
def after_sign_up_path_for(resource)
welcome_path
end
def after_update_path_for(resource)
welcome_path
end
end
尝试将其添加到Application Controller
中。查看设计文档以获得应用程序的最佳实现。如果允许用户从其帐户设置更改其用户名,则用户创建和用户更新可能都需要此功能。
https://github.com/plataformatec/devise#strong-参数
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) << [:username]
end
end