Using ruby variable for class name in HAML
I have the piece of code where I am trying to use a variable for a class name in HAML. Here it is:
- data_table(@installation_requests, nil, {:placeholder => ''}) do |installation_request, css_class|
%tr{:class => css_class}
I can't see anything wrong with it, RubyMine IDE doesn't pick an error either, it thinks that it is legitimate use of the variable. I'm getti开发者_JAVA百科ng the following error:
odd number of arguments for Hash
Can anyone point me to what's wrong with the code above?
What if you try:
- data_table(@installation_requests, nil, {:placeholder => ''}) do |installation_request, css_class|
%tr{:class => "#{css_class}"}
or if you are saving your views as view.html.haml:
- data_table(@installation_requests, nil, {:placeholder => ''}) do |installation_request, css_class|
<tr class="#{css_class}">
....stuff....
</tr>
The Hash can actually neatly pass an array passed to it into a sequence of keys and values.
For e.g.:
Hash["a", "apple", "b", "boy"] #=> {"a"=>"apple", "b"=>"boy"}
If you pass an odd number of arguments to be splat into key/value pairs...
Hash["a", "apple", "b", "boy", "c"] #=> odd number of arguments for Hash
So, I am pretty sure this is happening somewhere inside the data_table
method. So, please check if an array is being splat into Hash somewhere in the sequence of function calls!
精彩评论