How to prevent rails from processing edit fields_for differences from new_for fields

I am using rails3 beta3 and couchdb via couchrest. I am not using active recording.

I want to add multiple "sections" to the "Manual" and add and remove sections dynamically with a little javascript. I went through all of Ryan Bates's screencasts and they helped a lot. The only difference is that I want to store all the sections as an array of sections instead of individual sections. Basically like this:

"sections" => [{"title" => "Foo1", "content" => "Bar1"}, {"title" => "Foo2", "content" => "Bar2"}]

      

So, basically I need the params hash to make it look like this when the form is submitted. When I create my form, I do the following:

<%= form_for @guide, :url => { :action => "create" } do |f| %>
  <%= render :partial => 'section', :collection => @guide.sections %>   
  <%= f.submit "Save" %>
<% end %>

      

And my section partially looks like this:

<%= fields_for "sections[]", section do |guide_section_form| %>
  <%= guide_section_form.text_field :section_title %>
  <%= guide_section_form.text_area :content, :rows => 3 %>
<% end %>

      

Ok, so when I create a manual with sections, it works just fine as I would like. The params hash gives me an array of sections as I would like. The problem comes when I want to edit the guide / sections and save them again because rails insert the guide ID into the ID and name of each form field, which forces the params hash when the form is submitted.

Just to be clear, here is the output of the original form for the new resource:

<input type="text" size="30" name="sections[][section_title]" id="sections__section_title">
<textarea rows="3" name="sections[][content]" id="sections__content" cols="40"></textarea>

      

And this is how it looks when editing an existing resource:

<input type="text" value="Foo1" size="30" name="sections[cd2f2759895b5ae6cb7946def0b321f1][section_title]" id="sections_cd2f2759895b5ae6cb7946def0b321f1_section_title">
<textarea rows="3" name="sections[cd2f2759895b5ae6cb7946def0b321f1][content]" id="sections_cd2f2759895b5ae6cb7946def0b321f1_content" cols="40">Bar1</textarea>

      

How do I force rails to always use the new resource behavior rather than automatically adding an id to the name and value. Do I need to create a custom form designer? Is there another trick I can do to prevent the guides from displaying the guide ID? I've tried a bunch of things and nothing seems to work.

Thanks in advance!

+2


a source to share


1 answer


Ok, I think I figured out what works. Changing the first line of the partial to:

<%= fields_for "sections", section, :index => "" do |guide_section_form| %>

      



Everything seems to be fine. This way both the new and the right form looks the same under the hood, and the params hash works just the way I need it. If anyone sees something wrong with this or has some other alternative, please let me know.

+1


a source







All Articles