i using devise in rails 4 app. have member model (for devise) , user model contains profile info each member. member accepts nested attributes user. see form below new registration custom routes in routes.rb file.
my problem - works fine except validation of user attributes. if leave first name , last name blank, validates (but crashes this:

but other validations member (the devise model) work - end on /members page error message displayed should. unsure going on - shouldn't devise show error messages invalid nested attributes?
class members::registrationscontroller < devise::registrationscontroller # /resource/sign_up def new build_resource({}) self.resource.user = user.new respond_with self.resource end # post /resource def create super resource.user.ip_address = request.remote_ip resource.user.email = resource.email resource.user.save! end private def sign_up_params allow = [:provider, :uid, :email, :password, :password_confirmation, user_attributes: [:member_id, :email, :first_name, :last_name, :institution, :city, :country, :job_title, :about, :tag_list, :picture, :ip_address]] params.require(resource_name).permit(allow) end end member.rb
class member < activerecord::base devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :omniauthable, :trackable has_one :user, dependent: :destroy, autosave: true accepts_nested_attributes_for :user end user.rb
class user < activerecord::base belongs_to :member validates :first_name, presence: true validates :last_name, presence: true end in routes.rb
devise_for :members, controllers: {registrations: 'members/registrations', omniauth_callbacks: 'members/omniauth_callbacks', sessions: 'members/sessions' } in members/registrations/new.html.erb
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), html: { role: "form" }) |f| %> <%= f.fields_for(:user) |user_fields| %> <%= user_fields.text_field :first_name, autofocus: true, class: 'form-control', :placeholder => "first name" %> <%= user_fields.text_field :last_name, class: 'form-control', :placeholder => "last name" %> <% end %> <%= f.email_field :email, autofocus: true, class: "form-control", :placeholder => "email" %> <%= f.password_field :password, class: "form-control", :placeholder => "password" %> <%= f.submit t('.sign_up', :default => "sign up"), class: "btn btn-danger" %> <% end %> here log this
started post "/members" ::1 @ 2015-07-16 15:18:54 +0100 processing members::registrationscontroller#create html parameters: {"utf8"=>"✓", "authenticity_token"=>"sixk1ptvrxnhubddxatkfllg27fvqypq3muvo398thomomi1s3i2o3vbm gwrf6pvn1nfbr5vnr8ezkp6xnmzqw==", "member"=>{"user_attributes"=>{"first_name"=>"", "last_name"=>""}, "email"=>"slfwalsh@gmail.com", "password"=>"[filtered]"}, "commit"=>"sign up"} (0.2ms) begin transaction member exists (0.3ms) select 1 one "members" "members"."email" = 'slfwalsh@gmail.com' limit 1 (0.1ms) rollback transaction rendered devise/shared/_links.erb (9.9ms) rendered members/registrations/new.html.erb within layouts/static_home_page (19.8ms) rendered layouts/_static_login_header.html.erb (4.1ms) (0.1ms) begin transaction (0.1ms) rollback transaction completed 422 unprocessable entity in 1499ms
the reason why seeing page because save! triggers validation errors if validation of attributes failed , errors displayed in page. on other hand save doesn't display validation errors cancels transaction being saved if of validations failed. changing create method below should solve problem.
def create super resource.user.ip_address = request.remote_ip resource.user.email = resource.email resource.user.save end
Comments
Post a Comment