2013-03-20 162 views
0

我想实现一个使用rails3中的allows_nested_attributes_for功能来实现调查表单的嵌套窗体。调查有很多问题有很多答案。隐藏字段不是从助手在rails中生成

我想通过隐藏_destroy字段,并使用jQuery来设置它,当一个链接被点击。它工作正常,但当我尝试使用辅助方法进行相同操作时,隐藏字段不显示。

module ApplicationHelper 
    def link_to_remove_field(link_text, builder) 
    builder.hidden_field :_destroy 
    link_to link_text, '#', :class=>"remove_field" 
    end 
end 

这是我想使用的jquery函数。

$(function(){ 
    $('.remove_field').on('click', function(element){ 
    $(this).prev("input[type=hidden]").val("1"); 
    $(this).parent('.field').hide(); 
    return false; 
    }); 
}); 

这是部分从我打电话助手

<%= f.label :question %><br /> 
<%= f.text_area :question, :rows=>1 %> 
<%= link_to_remove_field("Remove question", f) %> 

的标签出现,但隐藏字段不显示。

<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> 

回答

1

link_to_remove_field只返回最后一行也就是link_to因为你忘了加上hidden_field。改变你的帮手

def link_to_remove_field(link_text, builder) 
    builder.hidden_field(:_destroy) + 
    link_to(link_text, '#', :class => "remove_field") 
end 

请注意调用此方法

link_to_remove_field(....).html_safe 
+0

当我把括号问题得到固定时,你可能需要调用html_safe:_destroy和的link_to。 不是没有括号的红宝石应该工作吗? 我是一个红宝石小白。为什么你必须在助手的每行之后添加'+'符号? – punitjajodia 2013-03-20 07:18:18