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.
Related
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.
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.
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)) %>
I've been running into issues with using .html_safe within .each loops. Here is how I may have something set up...
class Players < ApplicationController::Base
def index
#players = Players.all
end
end
In the view:
<% #players.each do |player| %>
<div>
<%= player.stat_code.html_safe %>
</div>
<% end %>
Now the stat_code would be a long HTML chunk of code... (I don't have an example for this case) But it's HTML.
Using the .html_safe does not seem to do anything while it's in a .each loop. Why does nothing happen and what other solution would you recommend?
Note: I also read that using .html_safe is bad! For this particular case I don't care.
I managed to accomplish my issue by setting up the view with sanitize instead of .html_safe like so...
<% #players.each do |player| %>
<div>
<%= sanitize player.stat_code %>
</div>
<% end %>
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.