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.
Related
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 %>
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 storing raw html from a contenteditable tag in my RethinkDB database.
Now I want to display the content after retrieving it.
html.eex
<div id="contentEditableText">
<%= for %{"contentText" => contentText} <- #contentText.data do %>
<div><%= "#{contentText}" %></div>
<% end %>
</div>
I can sucessfully retrieve it, but it's displaying the raw html itself.
The phoenix_html library provides a raw/1 function for this case. phoenix_html is included by default so you should just need to do:
<div id="contentEditableText">
<%= for %{"contentText" => contentText} <- #contentText.data do %>
<div><%= raw(contentText) %></div>
<% end %>
</div>
In my example I want RoR to display an image when I'm listening to Pearl Jam.
Winamp writes 'currently playing' info to np.txt.
<%= data = File.read("np.txt")
if data.include? "Pearl Jam"
<img src="space.jpg" alt="sagan"/>
end
%>
However I'm not sure how to get HTML tags to work inside RoR code.
I think what you want is:
<% data = File.read "np.txt" %>
<% if data.include? "Pearl Jam" %>
<img src="space.jpg" alt="sagan"/>
<% end %>
In ERB anything rendered outside of the <% %> tags is HTML.
ian.
You should use an image tag.
<%- data = File.read "np.txt" -%>
<%= image_tag("space.jpg", :alt => "sagan") if data.include? "Pearl Jam" %>
How do I place a link at the top of my page when the URL that it is pointing to is not determined until later down the page. In this example, I want to move Create and Edit Scenario links to the top of the page, but as you can see Edit Scenario depends on knowing the #scenario_id first.
<%= will_paginate #scens, :next_label => 'Older', :prev_label => 'Newer' %>
<div class="box">
<% for scenario in #scens %>
<% #created = scenario.created_at %>
<% #updated = scenario.updated_at %>
<% #scenario_id = scenario.id %>
<% if scenario.scenario_image.exists? %>
<%= scenario_image_tag(scenario) %>
<% end %>
<%= simple_format(scenario.description) %>
<% end %>
</div>
<% if session[:role_kind] == "controller" %>
<p>
<%= button_to "Create new scenario", :action => "create" %>
<% if #scens.size > 0 %>
<%= button_to "Edit scenario", :action => "edit", :id => #scenario_id %>
<% end %>
</p>
You can add the link at the top but you will need to programmatically access it later and then assign the URL to it. That needs some kind of reference or look-up capability, I'm thinking client-side javascript but that's as I don't know Ruby.
Alternatively you could create the link later when you have the URL and place the link at the top using CSS positioning. The actual position of all the DOM elements on the page need not match the order in which they are rendered.
One way to do this is to use a helper:
In your helper.rb file:
def stack_example(scens, &block)
html = 'Scenario Details'
edit_link = 'Edit Link'
yield html, edit_link
end
Then in your partial you could have something like:
<% stack_example(#scens) do |html, edit_link| %>
<%= edit_link %><br>
<%= html %>
<% end %>
Should output the following:
Edit Link
Scenario Details
I don't get it. Why do you create model in the view layer? Why wouldn't you create the model variables in the controller? Sth like:
class your_controller
def your_method
#scenario_id = ...
end
end
I think that your problem lays in the invalid MVC usage. Don't you think that all the #member #variables should be initialized before the view starts to render?