ruby on rails - How to display error messages in a multi-model form with transaction? -
two models, organization , user, have 1:many relationship. have combined signup form organization plus user organization signed up.
the problem i'm experiencing is: when submitting invalid information user, renders form again, should, error messages (such "username can't blank") user not displayed. form work when valid information submitted , display error messages organization, not user.
how should adjust code below error messages user displayed?
def new @organization = organization.new @user = @organization.users.build end def create @organization = organization.new(new_params.except(:users_attributes)) #validations require organization saved before user, since user requires organization_id. that's why users_attributs above excluded , why below it's managed in transaction rollbacks if either organization or user invalid. works desired. @organization.transaction if @organization.valid? @organization.save begin # executed next line in debugger (with invalid user info), correctly responds with: activerecord::recordinvalid exception: validation failed: email can't blank, email invalid, username can't blank, etc. @organization.users.create!(users_attributes) rescue # should perhaps add line here adds users errors memory? raise activerecord::rollback end end end if @organization.persisted? flash[:success] = "yeah!" redirect_to root_url else @user = @organization.users.build(users_attributes) # otherwise filled in information user gone (fields user empty) render :new end end
the form view includes:
<%= form_for @organization, url: next_url |f| %> <%= render partial: 'shared/error_messages', locals: { object: f.object, nested_models: f.object.users } %> <%= f.text_field :name %> # other fields <%= f.fields_for :users |p| %> <%= p.email_field :email %> # other fields <% end %> <%= f.submit "submit" %> <% end %>
the error messages partial follows:
<% object.errors.full_messages.each |msg| %> <li><%= msg.html_safe %></li> <% end %>
update: following steps rob's answer arrived @ errors partial below. still not display error messages user. added debugger responses inside code below , reason nested_model.errors.any?
returns false, while debugger inside controller (see above) return error messages user.
<% if object.errors.any? %> <div id="error_explanation"> <div class="alert alert-danger"> form contains <%= pluralize(object.errors.count, "error") %>. </div> <ul> <% object.errors.full_messages.each |msg| %> <li><%= msg.html_safe %></li> <% end %> </ul> </div> <% end %> <% if defined?(nested_models) && nested_models.any? %> # debugger: responds "local-variable" "defined?(nested_models)" , "nested_models.any?" returns true. <div id="error_explanation"> <ul> <% nested_models.each |nested_model| %> # debugger: "nested_model" has same values "nested_models.any?", expect. "nested_model.errors.any?" returns false, shouldn't. <% if nested_model.errors.any? %> #initially had "unless nested_model.valid?" errors user displayed on loading form page (new method). <ul> <% nested_model.errors.full_messages.each |msg| %> <li><%= msg.html_safe %></li> <% end %> </ul> <% end %> <% end %> </ul> </div> <% end %>
try adding validates_associated :users
under has_many :users
association in organization.
http://apidock.com/rails/activemodel/validations/classmethods/validates_associated
Comments
Post a Comment