Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3.2 Submitting has_many :through form Triggers Validations
    primarykey
    data
    text
    <p>Strange error and I've never come across this before. If I'm submitting a many to many form, it's triggering validation for one of the models in the has_many :through relationship. When I take out the validations, the form submits ok. Why are validations preventing a has_many :through form from submitting? Here is the info</p> <pre><code>class Client has_many :client_routes has_many :routes, :through =&gt; :client_routes #plus many validations below end class Route has_many :client_routes has_many :clients, :through =&gt; :client_routes end class ClientRoute belongs_to :client belongs_to :route </code></pre> <p>in my form to create routes I have a ul div that I use for drag and drop with the following hidden field. Those hidded fields are looped through an array of clients.</p> <pre><code>&lt;%= hidden_field_tag("route[client_ids][]", c.id) %&gt; </code></pre> <p>I have several of validation rules in the Client class. The form submits correctly when I comment out all of them. I have added validations one by one to try to see which one trips it up, but all of the validations seem to give me the issue.</p> <p>The validation error is as follows</p> <pre><code>1 error prohibited this route from being saved: There were problems with the following fields Clients is invalid </code></pre> <p>Here is the form where the partial is simply an empty ul for dragging and dropping:</p> <pre><code>&lt;%= form_for Route.new, :remote =&gt; true do |f| %&gt; &lt;p&gt; &lt;%= f.label :name %&gt; &lt;br/&gt; &lt;%= f.text_field :name %&gt; &lt;/p&gt; &lt;p&gt; &lt;%= f.label :mileage %&gt; &lt;br/&gt; &lt;%= f.text_field :mileage %&gt; &lt;/p&gt; &lt;p&gt; &lt;%= f.label :rate %&gt; &lt;br/&gt; &lt;%= f.text_field :rate %&gt; &lt;/p&gt; &lt;p&gt; &lt;%= f.submit %&gt; &lt;/p&gt; &lt;div class="assign_clients"&gt; &lt;%= render :partial =&gt; "client_routes" %&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre> <p>Here is the create action of the controller</p> <pre><code>def create @route = Route.new(params[:route]) if @route.save respond_with @route, :location =&gt; routes_url else flash[:notice]= "Not saved due to your incompetence" end end </code></pre> <p>Another test that kinda works is that I added the following which skips validation, but this result now skips validations that I have for the Route model.</p> <pre><code>if @route.save(:validation =&gt; false) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload