HAML index of array element each loop - haml

So, i want to type the index of array element with each loop, i have tried
- #characters = ['t','e','s','t'];
- #characters.each do |character, index|
%div{:class => "#{index}"} #{character}
but nothing happened.

Instead of using Array#each, use Enumerable#each_with_index to also get the collection object's index:
- #characters = ['t','e','s','t']
- #characters.each_with_index do |character, index|
%div{:class => index}= character
This will output:
<div class='0'>t</div>
<div class='1'>e</div>
<div class='2'>s</div>
<div class='3'>t</div>

Related

Rails+simple_form_for Change common id

I want to change form common name in rendering side
_from.html.haml
..
= f.simple_fields_for Image.new do |form|
= render 'avatar_fields', f: form
..
_avatar_fields.html.haml
..
= f.hidden_field :imageable_type
..
This is rendering like
<input id="product_image_imageable_type" name="product[image][imageable_type]" type="hidden">
But i want to render like this
<input id="product_logo_attributes_imageable_type" name="product[logo_attributes][imageable_type]" type="hidden">
I don't want to edit my '_avatar_fields.html.haml' screen. Because it's common html.
Any suggestion please..?
you can do some thing like
...
= f.hidden_field :imageable_type, input_html: {id: 'product_logo_attributes_imageable_type'}
...
I hope that this helps you

ejs, how to add dynamic attributes of html tag?

I use express.js + ejs, I have two cases:
1.
prev
But it give me an error: Could not find matching close tag for "<%="./nundefined/nError: Could not find matching close tag for "<%=".
I want to get
prevDisabledClass ? <a href=''>prev</a> : <a href='?page=<%=+page - 1%>'>prev</a>
2.
like above, but dynamic add href attribute to html tag <a>
I want to get this:
prevDisabledClass ? <a>prev</a> : <a href='?page=<%=+page - 1%>'>prev</a>
How can I solve these two problem?
For the first one you currently have this:
prev
You can't nest <%=, try this instead:
prev
For the second one it'd be almost exactly the same but you'd move the condition around more of the output:
<a<%- prevDisabledClass ? '' : (' href="?page=' + (page - 1) + '"') %>>prev</a>
Here I've used <%- instead of <%= to ensure the " doesn't get HTML encoded.
It might be clearer to ditch the ?: altogether:
<% if (prevDisabledClass) { %>
<a>prev</a>
<% } else { %>
prev
<% } %>
There's some duplication but it's much easier to read.

rails3 content_tag with static attributes

The following is being properly generated into HTML code
<%= content_tag(:span, (t 'hints.h'), :class => "has-tip", :title => (t 'hints.s') ) %>
But I am trying to generate
<span data-tooltip aria-haspopup="true" class="has-tip" title="title bla bla">translated h</span>
and have found no way to generate these span attributes data-tooltip aria-haspopup="true" They cannot be part of the options hash given one has only a name... and the second one has a dash which impedes from defining it as a symbol :aria-haspopup
I suggest that you use the following:
content_tag(:span, t('hints.h'), :class => 'has-tip', :title => t('hints.s'), :'aria-haspopup' => true, :'data-tooltip' => '')
Note that you can use the dash character in symbols if you enclose them in quotes.
The data attribute you could also specify as nested hash like :data => {:tooltip => ''} instead of :'data-tooltip' => '', use whatever you prefer.
As for the boolean attribute data-tooltip, setting the value to an empty string is as good as omitting it (and your best option with Rails 3 ;)). See also:
http://www.w3.org/html/wg/drafts/html/master/infrastructure.html#boolean-attributes

Pass variables into rails partial

So basically in my partial I have the following line of code
...
<%= " active" if current_user."#{prop_row}" == "repeat-x" %>
...
So I tried to pass in the following variables "prop_id", "prop_row" using:
<%= render :partial => "users/image_props/repeat", :prop_id => "mbr", :prop_row => "main_background_repeat" %>
I get the error
/Users/codyjames408/rails/moz/app/views/users/image_props/_repeat.html.erb:4: syntax error, unexpected tSTRING_BEG
...= ( " active" if current_user."#{prop_row}" == "repeat-x" );...
...
^
I think the errors because its appending a string instead of the row method. But I am pulling my hair trying to figure how to work around this.
I would love to turn this into a big helper method or something! I just don't know how...
If prop_row is a string, containing the name of an attribute, you ucan do this:
<%= " active" if current_user.attributes[prop_row] == "repeat-x" %>
Or use this:
<%= " active" if current_user.send(prop_row.to_sym) == "repeat-x" %>

Altering nested messages helper from div to unordered list

I am using a helper method from ryan bates railscasts on ancestry to display nested messages(code below works perfectly).
def nested_messages(messages)
messages.map do |message, sub_messages|
render(message) + content_tag(:div, nested_messages(sub_messages), :class => "nested_messages")
end.join.html_safe
end
The above bit of code nests the individual divs in a tree like structure. I would like to make this into an unordered list, so what i have done is this:
def nested_messages(messages)
messages.map do |message, sub_messages|
content_tag(:ul, :class => "") do
render(message)
content_tag(:li, :class => "nested_messages") do
nested_messages(sub_messages)
end
end
end.join.html_safe
end
The generated html looks fine, however the list items contain no values. Am i doing something wrong?
UPDATE
I would like the generated html to look like this:
<ul>
<li>Main Message</li> <!-- first message -->
<li>
<b>Message 1</b>
<ul>
<li>Message 1 subchild 1</li>
<li>Message 1 subchild 2</li>
</ul>
</li>
</ul>
UPDATE 2
I have changed it to this and it works, thanks to Dave:
def nested_messages(messages)
messages.map do |message, sub_messages|
#render(message) + content_tag(:div, sub_messages, :class => "nested_messages")
content_tag(:ul, :class => "") do
content_tag(:li, :class => "nested_messages") do
render(message) + nested_messages(sub_messages)
end
end
end.join.html_safe
end
You create a ul tag, then render the message. If you do that, what will your HTML look like?
Things inside a ul should be in a nested li: you just render the message.
You need to put it in an li tag so the unordered list has valid content.