将link_to更改为嵌套模型表单中的按钮

时间:2015-05-27 13:43:30

标签: ruby-on-rails link-to button-to

好的,我刚刚完成了从

实现嵌套的表单

http://railscasts.com/episodes/196-nested-model-form-part-1

http://railscasts.com/episodes/197-nested-model-form-part-2

这些视频很棒,但现在我想自定义链接并让它们看起来像按钮......

这是链接代码:

<p><%= link_to_add_fields "Add Vehicle", f, :vehicles %></p>

实际上链接到辅助方法:

  def link_to_add_fields(name, f, association)
    new_object = f.object.class.reflect_on_association(association).klass.new
    fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
      render("shipments/partials/" + association.to_s.singularize + "_fields", :f => builder)
    end
    link_to_function(name, "add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")")
  end

如何将其变成按钮

以下是我使用的按钮代码示例:

<button id="add_button" class="btn btn-primary pull-right" style="margin-right: 15px;">Add Another</button>

我曾尝试在链接中添加一个类

试过这个:

<%= button_to_remove_fields "Remove", f, :class=>"btn btn-primary pull-right" %>

得到了这个

wrong number of arguments (3 for 2)

1 个答案:

答案 0 :(得分:1)

<%= link_to_remove_fields "Remove", f %>