Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - Passing an option to a method

I have a help method that looks a something like this:

  def html_format(text, width=15, string="<wbr />", email_styling=false)

    if email_styling
       ...... stuff
    else
       ...... stuff
    end
       ...... stuff
  end

I'm having problems sending email_styling as true. Here is what I'm doing in the view:

<%= html_format(@comment.content, :email_styling => true) %>

Am I passing true incorrectly? Thanks

like image 420
AnApprentice Avatar asked Feb 21 '11 19:02

AnApprentice


1 Answers

You are not passing it correctly. You need to do the following:

<%= html_format(@comment.content, 15, '<wbr />', true) %>

Alternatively you can use an options hash to pass your parameters:

def html_format(text, options = {})
  opt = {:width => 15, :string => '<wbr />', :email_styling => false}.merge(options)

  if opt[:email_styling]
    ...
  end
end

So that you can make your call like this:

<%= html_format(@comment.content, :email_styling => true) %>
like image 68
Pan Thomakos Avatar answered Sep 30 '22 14:09

Pan Thomakos