We have answer of your question!

100% solved queries, no empty question

Question: sending multiple parameters to another function in ruby


I'm trying to create a function that adds some functionality to the link_to function in rails. What I'd like it to do is simply to add a class to it. What I have so far:

def button_link(*args)
    link_to(*args.push(class: 'btn'))

Problem is that if I now add another class to the button_link function it doesn't work.


<td class='button'>
    <%= button_link "Show", category_path(item), class: "btn-primary" %>

I get the following error: wrong number of arguments (4 for 3). How can I do this correctly?

Question author Nayish | Source



link_to has 4 method signatures.. This is the one used most often.

Below we check to see if a class was already sent in -- and because of how HTML classes work, we want to have multiple classes, which are space-separated values.

  def button_link(body, url, html_options={})
    html_options[:class] ||= ""
    html_options[:class] << " btn"
    link_to body, url, html_options

The other method signatures can be viewed http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to

Answer author Jesse-wolgamott

Tickanswer.com is providing the only single recommended solution of the question sending multiple parameters to another function in ruby under the categories i.e ruby-on-rails , ruby , ruby-on-rails-4 , . Our team of experts filter the best solution for you.

Related Search Queries:

ruby multiple arguments in method ,

You may also add your answer

Thanks for contributing an answer to Tick Answer!