ruby - Rails map one field in form to two fields in model -
i've seen lot of posts how map 2 form fields 1 model field, mapping 1 form field provide answers 2 (or more) model fields? have form users gives field, last_name, user. want default password user last_name (and have password_confirmation set up, needs last_name). how this?
form:
<div class="row"> <div class="col-md-6 col-md-offset-3"> <%= form_for(@user) |f| %> <%= f.label :first_name %> <%= f.text_field :first_name, class: 'form-control' %> <%= f.label :last_name %> <%= f.text_field :last_name, :password, :password_confirmation, class: 'form-control' %> <%= f.label :email %> <%= f.email_field :email, class: 'form-control' %> <%= f.hidden_field :access_level, :value => "chair" %> <%= f.label :phone %> <%= f.text_field :phone_number, :id => "phone", class: 'form-control' %> <p> </p> <div class="col-md-12"> <%= f.submit "add chair", class: "btn btn-primary" %> </div> <% end %> </div> </div>
user_controller.rb
def create @user = user.new(user_params) if @user.save log_in @user current_user flash[:success] = "welcome penn model congress!" redirect_to after_sign_in_path else render 'new' end end private def user_params params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :access_level, :phone_number) end end
i add before_validation
callback user
model this:
# in app/models/user.rb before_validation :set_default_password private def set_default_password self.password ||= last_name self.password_confirmation ||= last_name end
Comments
Post a Comment