Rails Template Inline Code not running - ruby-on-rails

I have a ruby on rails html.erb file with inline code in it like below. However, the code doesn't always run when somebody loads the page. It only prints to stdout the first time the page is loaded.
<%puts "TESTING"%>
Does rails cache my html.erb pages somehow? Is there a way to turn it off?

If you want to output something you need to use <%= %> instead of <% %>
<%= "TESTING" %>
<%= %> - Evaluate and print the output
<% %> - only evaluate

Try below code:
<%= "TESTING" %>
<%= %> is used to print the text in html.erb file

Related

cant translate html code in ruby on rails

im new for ruby on rails. i have 3 question
how to i translate the html code
in controllers i set
def index
#sentence = "Halo<br>worlds";
end
in index.html.erb display i set
<%= #sentence %>
but display
Halo<br>world
how do i make it like this
halo
world
how do i set onclick in submit button? i already tried set the button like this <%= submit_tag "back", onclick: "window.history.back();" %>
but the result is not go to previous page. but excetude the form..
how i can make an validation form if the validation code is in controllers. not in model
Rails automatically escapes all HTML in ERB templates. To prevent this escaping you can use the Rails ERB extension <%== %>. So in your case it would be:
<%== #sentence %>
See this question for some further discussion:
What does <%== %> do in rails erb?
Replace <%= #sentence %> with below content for a simple solution.
<% #sentence.split("<br/>").each do |word| %>
<%= word %><br/>
<% end %>
for the second qsn, check the validations in validateform() function and then respond with true or false accordingly.
<%= button_to_function "Submit", "validateform()" %>

Why does commented-out ERB still throw errors?

I have an HTML file in this format:
html
more html
<%= valid erb %>
<!--
<%= incomplete_erb_with_bugs %>
-->
But I still get an exception page, because of the buggy ERB. But shouldn't commenting that section out cause that part not to be read by the browser? Is there another HTML method for actually preventing the browser from reading code?
Because its not commented.
But shouldn't commenting that section out cause that part not to be
read by the browser?
The browser does not execute Ruby or ERB - the server does before it sends the resulting HTML document to the browser.
ERB is ruby code imbedded in a file that contains literal text. The interpreter does not care about anything except the code in "erb tags".
This is just literal text
<%# this is a ruby line comment - the code below is executed: %>
<% bar do %>
<%= foo %>
<% end %>
The rest is just placed in the buffer. This is just like PHP or any other embedded language.
So a HTML comment (or CSS or JS for that matter) does not effect the ERB interpreter in any way. The interpreter does not really know or care that its creating HTML.
Is there another HTML method for actually preventing the browser from reading code?
The browser does not execute Ruby code. It just builds a document from whatever you send in the response.
So use a ruby comment <%#= incomplete_erb_with_bugs %> which will prevent the code from being executed - and it will never get sent to the browser.
you're commenting out html, but the ruby code inside it still gets evaluated. you need to comment out the code, which is done like this:
<%#= incomplete_erb_with_bugs %>
you need to comment the actual line, not the html surrounding it
html
more html
<%= valid erb %>
<%= incomplete_erb_with_bugs %>
you can also use if statements
<% if false %>
<%#= incomplete_erb_with_bugs %>
<% end %>

What is the difference between <%= and the "puts" command in rails?

I have a segment of code:
<% #public_address.each do |key| %>
<%= key["address"]["address"] %>
<% end %>
This displays the keys properly, but this code
<% #public_address.each do |key| %>
<% puts key["address"]["address"] %>
<% end %>
displays nothing. What gives? What's the difference between the two?
The <% %> and <%= %> are used in erb to execute ruby code when rendering a template.
Erb is the default template engine in rails.
Difference between <% %> and <%= %>
<% %> Will evaluate the ruby code it contains, but "silently".
Meaning that no output is going to be printed on the rendered page.
<%= %> on the other end, evaluates the ruby it contains and
renders the result on the rendered page.
What's the difference between <% code %> and <%= code %> in Rails erb?
What's puts?
Puts is simply a method from Ruby that is used to print a string at runtime. It has nothing to do with erb templates.
In your first bit of code <%= key["address"]["address"] %>, the <%= %> is rails syntax for evaluating the code inside and returning the value.
In your second bit of code <% puts key["address"]["address"] %>, you use <% %>, which doesn't return an evaluated rails statement. Furthermore, puts is a method that outputs whatever follows it to the stout object. In a command line program, that means printing out to the terminal screen, but in a web app you aren't working with a terminal screen. You are working with controllers and view templates, so it is necessary to use the evaluative <%= %> if you want to return values that will be displayed in the view.

Use template from DB in usermailer and interpret ruby code inside the string?

Ok so this one is hurting my brain:
I have something like this in send_invoice_html.erb which is one of my user_mailer views:
<% if #cust_inv_template %>
<%=raw #cust_inv_template.cust_mail_invoice_template%>
<%else%>
So I have the user setup their email template inside another view, save that to the database, and then I want to say "if there is a template, use that, else, use my default".
Inside my template is some HTML and some Rails code:
<html>
Aloha <%= #user.first_name %>,<br />
<p>Please find attached your invoice for Job #<%= #job.job_number %> which we completed on <% ed = Date.parse(#job.end_date.to_s);
endDate = Date.strptime(ed.to_s, "%m-%d-%Y") %><%= endDate.to_s %>.</p>
<p>If you have any questions, please call us at <%= number_to_phone(#admin.phone_number) %> or email us at <%= #admin.email %></p>
<br />Thanks,
<br/><%= #admin.name %>
</html>
So when the message gets sent to my mailbox (Gmail) - I see :
Aloha <%= #user.first_name %>,
Please find attached your invoice for Job #<%= #job.job_number %> which we completed on <% ed = Date.parse(#job.end_date.to_s); endDate = Date.strptime(ed.to_s, "%m-%d-%Y") %><%= endDate.to_s %>.
If you have any questions, please call us at <%= number_to_phone(#admin.phone_number) %> or email us at <%= #admin.email %>
TEST TEST TEST TEST TEST
Thanks,
<%= #admin.name %>
Ergo - my HTML is being respected just fine with the <%=raw part (I tried html_safe previously) - but the Rails code is being spit out as-is. I want that Rails-y goodness to be interpreted BEFORE the message is sent.
When I take out the if else stuff and just use the exact same template the Rails code IS respected.
Which means there has to be some kind of "spit out what's inside this string var AND if there's any cool rails stuff in there go ahead and interpret that" method in some class :)
Anyone know what that is?
raw is about not escaping the HTML, but it will not interpret your ruby code. To interpret your ruby code you need to use evaluate the erb template.
<%=raw Erb.new(#cust_inv_template.cust_mail_invoice_template).result %>
Check the docs for more info.

Different tags in ERB

I'm just getting started with Ruby and Ruby on Rails, so excuse me if this is a simple question. I've noticed that in some ERB files, there is a difference to using <%= %> and <% %>, but what is the difference?
Thanks!
The difference is as follows:-
<%= %> would execute and print the value of rails code written inside and <% %> would just execute the rails code.
Thanks, Anubhaw
<%= %> in ERB equals to <?php echo ?> in PHP.

Resources