Model validation errors not appearing for nested model in simple_form

Go To StackoverFlow.com

2

So I have a simple form for entering "brand" and "model" through a single submit button, as below:

<%= simple_form_for @brand, :html => { :class => 'form-horizontal' } do |m| %>
  <fieldset>
    <legend><%= controller.action_name.capitalize %> /Brand</legend>
        <%= m.input :name %>

        <%= m.simple_fields_for :models, Model.new do |p| %>
      <%= p.input :name %>
    <% end %>

    <div class="form-actions">
      <%= m.submit nil, :class => 'btn btn-primary' %>
      <%= link_to 'Cancel', brands_path, :class => 'btn' %>
    </div>
  </fieldset>
<% end %>

I have name:string in my schema for both brand and model.. and validates_presence_of :name in both models.. The form DOES work for creating the brand and model simultaneously but my error "can't be blank" only shows up for the brand field.

Thanks for any help with this issue.

2012-04-04 08:31
by Abram
Do you have acceptsnestedattributes_for :models in your brand model - DanS 2012-04-04 08:43
Yes. I do.. and the code works.. but say I leave 'brand' and 'model' fields empty, "can't be blank" shows up only next to the 'brand' text field - Abram 2012-04-04 08:49
Sounded promising.. but nope, didn't do the trick.. well it didn't break anything, but still no error message next to the model field. The validation does seem to be working, even without validates_associated.. If I just fill in the "brand name" field and click submit the new brand will not be saved in the db. In other words I must enter something in both fields for a brand to be saved, which makes me think validation is ok - Abram 2012-04-04 08:57
Add <%= m.error_notification %> to the top - DanS 2012-04-04 09:02
undefined local variable or method `m' for #<#:0xa906f00 - Abram 2012-04-04 09:11
let us continue this discussion in chatDanS 2012-04-04 09:12
Realized what I was doing wrong. Needed to ALSO put @model = Model.new inside of the controller and put <%= m.simple_fields_for :models, @model do |p| %>Abram 2012-04-04 09:40


2

Realized what I was doing wrong. Needed to ALSO put @model = Model.new inside of the controller and put <%= m.simple_fields_for :models, @model do |p| %>

2012-04-04 20:41
by Abram
Ads