# Rails form partial help - nested resource

**URL:** <https://www.ruby-forum.com/t/rails-form-partial-help-nested-resource/186169>\
**Category:** Rails\
**Created:** [April 1, 2010, 3:45pm UTC](https://www.ruby-forum.com/t/rails-form-partial-help-nested-resource/186169 "2010-04-01T15:45:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [April 1, 2010, 3:45pm UTC](https://www.ruby-forum.com/t/rails-form-partial-help-nested-resource/186169/1 "2010-04-01T15:45:23Z")

</div>

Hi all,

I apologize for the funky subject of this post.

I have a partial I created while recreating a railscasts example (nested  
resources part 2 -  
[#197 Nested Model Form Part 2 - RailsCasts](http://railscasts.com/episodes/197-nested-model-form-part-2)) and I’ve  
come to a halt.

What I want to do is force a question to be asked if this is a new  
“survey”. Basically I want to build one question in the new action and  
display it in the form and remove the link\_to\_remove\_fields helper - BUT  
I also want to be able to add AND remove new questions. While making  
sure that the inital question cannot be removed.

Hopefully this makes sense. Below is my partial code:

```auto

<div class="fields">
  <%= f.error_messages %>

  <p>
    <%= f.text_field :content %>

    <% unless f.object.new_record? %>
    <%= link_to_remove_fields "remove", f %>
    <% end %>
  </p>
</div>

```

I added \<% unless f.object.new\_record? %\> to omit the remove link on the  
new action and this works. However when someone clicks the ‘add  
question’ (code not shown but identical to the railscast code) it  
display the fields but obviously also omits the remove link for the  
newly added questions.

How can I omit the first remove link but show the remove link for newly  
added questions in the new action? Please let me know if you need more  
info!

Thank you!  
-Tony

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [April 1, 2010, 5:30pm UTC](https://www.ruby-forum.com/t/rails-form-partial-help-nested-resource/186169/2 "2010-04-01T17:30:01Z")

</div>

Hi Tony,

Hope ur using Jquery

Add the class to the remove link and add the following line of code

> \<%= f.error\_messages %\> 
> 
> \<%= f.text\_field :content %\>
> 
> ```
> <% unless f.object.new_record? %>
> <%= link_to_remove_fields "remove", f,:class=>"remove" %>
> <% end %>
> 
> ```

$(document).ready(function(){  
$(“.remove”).eq(0).remove()  
})

Tony T. wrote:

> Hi all,
> 
> I apologize for the funky subject of this post.
> 
> I have a partial I created while recreating a railscasts example (nested  
> resources part 2 -  
> [#197 Nested Model Form Part 2 - RailsCasts](http://railscasts.com/episodes/197-nested-model-form-part-2)) and I’ve  
> come to a halt.
> 
> What I want to do is force a question to be asked if this is a new  
> “survey”. Basically I want to build one question in the new action and  
> display it in the form and remove the link\_to\_remove\_fields helper - BUT  
> I also want to be able to add AND remove new questions. While making  
> sure that the inital question cannot be removed.
> 
> Hopefully this makes sense. Below is my partial code:
> 
> ```auto
> 
> > <div class="fields">
> > <%= f.error_messages %>
> > 
> > <p>
> > <%= f.text_field :content %>
> > 
> > <% unless f.object.new_record? %>
> > <%= link_to_remove_fields "remove", f %>
> > <% end %>
> > </p>
> > </div>
> > 
> ```
> 
> I added \<% unless f.object.new\_record? %\> to omit the remove link on the  
> new action and this works. However when someone clicks the ‘add  
> question’ (code not shown but identical to the railscast code) it  
> display the fields but obviously also omits the remove link for the  
> newly added questions.
> 
> How can I omit the first remove link but show the remove link for newly  
> added questions in the new action? Please let me know if you need more  
> info!
> 
> Thank you!  
> -Tony

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [April 1, 2010, 6:39pm UTC](https://www.ruby-forum.com/t/rails-form-partial-help-nested-resource/186169/3 "2010-04-01T18:39:00Z")

</div>

babu nair wrote:

> Hi Tony,
> 
> Hope ur using Jquery
> 
> Add the class to the remove link and add the following line of code
> 
> > \<%= f.error\_messages %\> 
> > 
> > \<%= f.text\_field :content %\>
> > 
> > ```
> > <% unless f.object.new_record? %>
> > <%= link_to_remove_fields "remove", f,:class=>"remove" %>
> > <% end %>
> > 
> > ```
> 
> $(document).ready(function(){  
> $(".remove").eq(0).remove()  
> })

Thank you!!

Using jquery now! Is there really that big of a difference? I know  
everybody raves about it - more lightweight is all?

For others to learn here’s what I did.

1. I put babu’s script in the bottom of the form (eventually I’ll put it  
in the head via a yield).
2. Put the link\_to\_remove\_fields in a span with class remove (\<%= link\_to\_remove\_fields “remove”, f %\>)

That’s it! Hope this helps someone else out there.
