Reputation: 92
I am trying to implement a nested form using the allows_nested_attributes_for feature in rails3 to implement a survey form. Survey has many questions has many answers.
I want to pass the :_destroy field as hidden and use jQuery to set it when a link is clicked. It was working fine but when I tried doing the same using a helper method, the hidden field doesn't show up.
module ApplicationHelper
def link_to_remove_field(link_text, builder)
builder.hidden_field :_destroy
link_to link_text, '#', :class=>"remove_field"
end
end
This is the jquery function I want to use.
$(function(){
$('.remove_field').on('click', function(element){
$(this).prev("input[type=hidden]").val("1");
$(this).parent('.field').hide();
return false;
});
});
And this is partial from which I am calling the helper
<%= f.label :question %><br />
<%= f.text_area :question, :rows=>1 %>
<%= link_to_remove_field("Remove question", f) %>
The a tag appears but the hidden field doesn't show.
<textarea cols="40" id="old_question_set_old_questions_attributes_2_question" name="old_question_set[old_questions_attributes][2][question]" rows="1"></textarea>
<a href="#" class="remove_field">Remove question</a>
Upvotes: 0
Views: 114
Reputation: 29599
You link_to_remove_field
is only returning the last line which is the link_to
because you forgot to add the hidden_field
. change your helper to
def link_to_remove_field(link_text, builder)
builder.hidden_field(:_destroy) +
link_to(link_text, '#', :class => "remove_field")
end
please take note that you may need to call html_safe
when calling this method
link_to_remove_field(....).html_safe
Upvotes: 1