How do I iterate over a range in ERB? - html

I'm trying to generate a dropdown with the alphabet in it. Weird, I know, I'm making an old school arcade style name selector for a game (3/4 letters) selectable with arrow keys.
So far I've got this:
<%= select_tag(:letters) do %>
<% ('A'..'Z').each do |letter| %>
<%= content_tag(:option, letter, value: letter) %>
<% end %>
<% end %>
I'm getting the <select> tag, but the option tags are not generated. I'm sure it's just a small syntax error but I'm stumped.

The following should work:
<%= select_tag(:letters, options_for_select(('A'..'Z').to_a)) %>

Related

Ruby on Rails: How do I assign a "step" attribute on a time_field to step in 30 minute intervals?

I'm using form_with to generate a html form using Ruby on Rails. In this form_with, I'm using a fields_for to generate another section of forms that are attributes of the first form.
To make things simple, though, I simply want to know how I can restrict time selections to be in 30 minute increments.
For example, I am doing:
<%= form.fields_for employee_jobs do |assign_job| %>
<%= assign_job.time_field :time_start %>
<%= end %>
How can I make it so I get a "step" attribute in the html time input node when I'm done? Right now, I'm trying to do:
<%= form.fields_for employee_jobs do |assign_job| %>
<%= assign_job.time_field(:time_start, :step=>600) %>
<%= end %>
But it's not giving me the desired result.
I found out what I needed to do. Instead of using a time_field input, you can use the time_select input, and specify steps through the minute_step keyword.
Instead of:
<%= form.fields_for employee_jobs do |assign_job| %>
<%= assign_job.time_field :time_start %>
<%= end %>
You can do:
<%= form.fields_for employee_jobs do |assign_job| %>
<%= assign_job.time_select :time_start {:minute_step => 30} %>
<%= end %>
And this solved my problem.

How to put array in an I18n in html.erb

I am working with a t('footer.card.type.name') where I want to replace name with the array list. t('footer.card.type.name') by the way, is to show different languages in different I18n.
Currently I am stuck with placing |type| into t('footer.card_type.%{type}'). This I am sure it does not work. I am just experimenting on ways to making it work.
<%= link_to t('footer.card_type.all'), credit_cards_path %>
<% ['cash_back', 'islamic', 'petrol', 'reward', 'travel', 'no_annual_fee', 'premium', 'balance_transfer', 'promo'].each do |type| %>
<%= link_to t('footer.card_type.%{type}'), credit_card_type_path(sub_type: type.gsub('_','-')) %>
<% end %>
I have found a work around by adding .concat(type) after the t('footer.card_type.'). It successfully returns the translation of both languages. Hope this code can help someone.
<% ['cash_back', 'islamic', 'petrol', 'reward', 'travel', 'no_annual_fee', 'premium', 'balance_transfer', 'promo'].each do |type| %>
<%= link_to t('footer.card_type.'.concat(type)), credit_card_type_path(sub_type: type.gsub('_','-')) %>
<% end %>

eRB and multiple statements syntax?

I have the following in an html.erb file, using link_to to get a hyperlink and t() to internationalize my text. But it looks very clunky:
<p><%= t('session.new_user') %><%= link_to(t('session.signup_now'), signup_path) %></p>
Splitting onto multiple lines seems wrong since the text will all appear on the same line on screen but is there a better syntax to avoid the two consecutive <%= %> blocks?
I would probably go for line breaks:
<p>
<%= t('session.new_user') %>
<%= link_to t('session.signup_now'), signup_path %>
</p>
or you could set variables before the actual code
<% new_user_text = t('session.new_user') %>
<% link = link_to t('session.signup_now'), signup_path %>
<p><%= new_user_text %><%= link %></p>
or you could set instance variables in the controller. I wouldn't like that for view stuff like this.
Extra: if you like tidy code you may like haml
%p
= t('session.new_user')
= link_to t('session.signup_now'), signup_path
now it is actualle readable!
You can add a hyphen before the closing tag to prevent a newline being appended to the output.
<% ... -%>
Please note that this feature is Rails specific.

How to not show a line break when blank?

In my Ruby app I'm using the following code in my view:
<% if post.image.present? %>
<%= image_tag post.image_url(:thumb).to_s %>
<% else %>
<%= "" %>
<% end %>
If there's no image then it shows a blank which works fine.
The problem is that I do not want to show a line-break. Instead of <%= "" %>, is there something I can use to do that?
I'd do it like this. Instead of a massive if/else statement, use the Ruby one-liner, and instead of an empty string, don't add anything if it's null:
<%= image_tag post.image_url(:thumb).to_s if post.image.present? %>
Note: If it's still adding a line-break, that's probaby something in your CSS. Nothing about this code should give you a line break.

Inserting rails into HTML file

When writing an HTML file, why use <%= INSERT RAILS HERE %> vs. <% INSERT RAILS HERE %>
<%= %> emits a string, <% %> runs code.
On the pedantic side, you're writing an ERb template, not an HTML file--the syntax is the same whether it's a template for HTML, JS, or whatever.
The ERB docs provide additional (but not complete) information.
<%= %> will return value and display in your page. Assume that you have person.name = 'Dark'
<%= person.name %>
will display Dark in your web page.
<% %> will not return any value to your page. It just embed simple ruby code. Usually used with `control statement'.
<% if person.present? %>
<span><%= person.name %></span>
<% end %>
When we use <%= %> it simply displays the value returned, on the html page.
<% %> executed the code but doesn't dispaly it on the html page.