Skip to content

scoping collection for nested form (semantic_fields_for) #881

kzli216 opened this Issue Aug 16, 2012 · 2 comments

3 participants

kzli216 commented Aug 16, 2012

I'm using formtastic in a form partial to display tasks from my project edit form (where a project has many tasks and accepts nested attributes for tasks).

The problem comes up, however, when I try to feed a scoped collection through semantic_fields_for. I'm trying not to show all of the child elements but rather only the child elements where the status of the task != submitted. If I use

<%= f.semantic_fields_for :tasks, @project.tasks do |task| %>

it shows all of the tasks, and on a failed submission, my inline error messages as well. When I scope the collection, however,

<%= f.semantic_fields_for :tasks, @project.tasks.unsubmitted do |task| %>

on a failed submission, I no longer see my error messages. The validations still work as they should, and I only see the task forms where status != submitted (the scope is working)--the only thing that I've lost is my error messages and I'm unsure why that's happening/how to avoid it.

Are there any ideas?


Hi, I think you're mistaking what the second argument in semantic_fields_for actually does. For the purposes of this issue, I think it's best to describe it as an association, rather than a collection. You should be using the first example:

<%= f.semantic_fields_for :tasks, @project.tasks do |task| %>

To actually scope/reduce the collection down to unsubmitted, you should to it with the :collection option, on an actual input:

<%= f.semantic_fields_for :tasks, @project.tasks do |task| %>
  <%= task.input :whatever, :collection => @project.tasks.unsubmitted %>
<% end %>

Closing for now, feel free to reopen if I'm missing something!


Just FYI. I think you're talking about two different things.

To solve the original issue I put <% f.object.valid? %> at the top of the partial and the errors show up.

The problem is that when you explicitly pass in a collection to semantic_fields_for that collection gets pulled from the DB as fresh model instances so the objects don't have the errors on them.

Calling .valid? on the object from within the partial populates errors on f.object.

I need to pass the collection so that the order is what I need. Can't put the order as a default scope either (which would work great if I could).

It's hackish but it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.