'Adding a button to <%= link_to_add_fields "Add A Present", f, :presents %> in Rails?

I can't figure out how to add a button to a link_to_add_fields as in

<%= link_to_add_fields "Add A Present", f, :presents %>

in Rails App I'm making.

I tried adding, class: "btn btn-mini btn-info" everywhere, but I keep getting an error about number of arguments.

It works for example in this line:

<%= f.link_to_remove "Remove this present", class: "btn btn-mini btn-info" %>

Thanks in advance.



Solution 1:[1]

I am assuming you made this helper from the rails cast episode http://railscasts.com/episodes/197-nested-model-form-part-2.

In your application_helper.rb, you have defined link_to_add_fields. Probably something like this.

def link_to_add_fields(name, f, association)

change it to this

def link_to_add_fields(name, f, association, locals={})

Then on the return statement.

link_to_function(name, "add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")", class: locals[:class])

Finally, use your new method like so

<%= link_to_add_fields "Add A Present", f, :presents, class: "btn btn-mini btn-info" %>

Solution 2:[2]

Based off @Aeisme's response I had to hack together a solution for Rails 4.1 users.

application_helper.rb

def link_to_add_fields(name, f, association, locals={})

return value for link_to_add_fields function

link_to(name, '#', class: [locals[:class], "add_fields"], data: {id: id, fields: fields.gsub("\n", "")})

Solution 3:[3]

Allen's answer worked for me! You could also add the button class directly in your helper method.

see helper method example

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 allen
Solution 2 pyRabbit
Solution 3 quickfox