tim_xyz
tim_xyz

Reputation: 13571

Why is value passed to method in optional hash a `nil`? (Ruby)

I've created a custom method and am trying to pass a value to it via an optional hash.

But the error message indicates to me that the method is not actually receiving the value I am passing, hence nil.

View:

<%= f.custom_form :height, { item: ‘weight’} %>

Method:

def custom_form(type, additional_items = {} )
  class_array = [‘form_control’]

  if additional_items
    class_array << additional_items[:item]
  end
end

Error:

no implicit conversion of nil into String

But I'm not sure why this is occurring.

Apologies in advance if this is a newbie question.

Upvotes: 0

Views: 119

Answers (1)

SteveTurczyn
SteveTurczyn

Reputation: 36880

This line is ambiguous

<%= f.custom_form :height, { item: ‘weight’} %>

because it can be interpreted as one argument and a block

Try using explicit parentheses and you don't need the hash braces, they'll be assumed

<%= f.custom_form(:height, item: ‘weight’) %>

Upvotes: 1

Related Questions