Ruby on rails .erb file - html

My .erb file does not execute the code inside <%= %> as ruby code. For example::
<%= puts "almost"%>, when rendered is just <%= puts "almost"%>. The erb engine does not even process anything. This means that NOTHING inside <%= %> is being processed .Any help will be appreciated.

You are confusing erb expressions with Ruby code. Use <% %> to have arbitrary Ruby code executed.
<% puts "almost" %>

Anything inside <% %> will be evaluated as ruby code, and anything inside <%= %> will be outputed as html. So when you do <%= puts "almost" %> it is outputded directly as HTML. You can solve this using
<% puts "almost" %>
or
<%= "almost" %>.

Related

Embedding Ruby into a nanoc site doesn't work

When I attempt to loop through sorted_articles() in Ruby, the embedded code just stays the same and doesn't get compiled in the output when I run nanoc compile. Why is this? Am I embedding it wrong? I've tried all the variations below:
<%= for item in sorted_articles()
<p>item[:title]</p>
end %>
also
<% for item in sorted_articles() %>
<p>item[:title]</p>
<% end %>
and
<% sorted_articles().each do |item| %>
<p>item[:title]</p>
<% end %>
and finally
<%= sorted_articles().each do |item|
<p>item[:title]</p>
end %>
I also followed a tutorial here pretty much exactly as I found it, but still no luck! The routing is working, so are the rules, and everything else. Just this damn loop!
Thanks. The source can be found on gitlab and the live site can be found here
The compile rule for all .html files wasn't being passed through the erb filter, and as Sergio mentioned in the comments, the code was being ignored. Adding the filter :erb fixed the issue

form_tag inside a form_tag - In rails

I wanted to use two form_tag in rails like this,
<%= form_tag %>
<%= form_tag %>
................
<%= submit_tag %>
<% submit_tag %>
But the action for inside form_tag taking first form_tag's action...!!!
For both form_tag assign the different action?
HTML does not allow nested forms, thus it's reasonable that the helpers are not designed to support nesting.
what you need is called *Nested Forms * take a look at the rails cast #196 Nested Model Form Part 1
Here is a link to a gem that you can use to help you out https://github.com/ryanb/nested_form

How do i write my erb file without skipping lines

i have the t function witch return some text.
so on my erb file i have something like that:
<%= t 'content1' %>
<%= t 'content2' %>
and the html output is something like that:
"text of the content 1"
"text of the content 2"
I would like to output something like that:
"text of the content 1" "text of the content 2"
thanx
ERB has an option in the closing tag whether to have a newline after it. Just add a dash:
<%= t 'content1' -%>
<%= t 'content2' %>
Erb is a little overrated compared to other template languages out there. But, the good thing is it allows you to control the output. As #DMG pointed out add a '-' to your tags but there is also another point I wanted to show.
<%- method(...) -%>
Will not show any lines on output. While this will create a blank line:
<% method(...) -%>
Just something to keep in mind if your writing templates for scripts, unit tests, rake task, or anything else outside of Rails/ActionView
<%= "#{t('content1')} #{t('content2')}" %>
just do:
<%= "#{t('content1')}#{t('content2')}" %>

Trying to convert text inside a text_area to HTML

I'm doing a sort of blog in Rails, and when I put something like
Hello, <b> how are you? </b>
inside the text_area and I send it, I see that it was saved as a plain text, and the HTML inside it is not recognized.
The only thing I want is that every single HTML tag that I put in the text_area be interpreted as HTML and not as a plain text.
For example, the first code should be shown in my page like this:
Hello, how are you?
This is the code of the post's view new (in HAML):
= form_for #post do |f|
.form
.flabel= f.label :title
= f.text_field :title
.flabel= f.label :content
= f.text_area :content
.submit= f.submit "Create"
How could I do this without HTMLArea or things like that?
Thank you!!
What about raw helper? Try the folliwing <%= raw #post.content %> in your view.
I think <%= #post.content.html_safe %> would also work.

What's the difference between "<%" and "<%=" in embedded VBScript?

I am working on a code base which as VBScript code embedded in HTML. I've noticed the following two different tags around said lines of code
<%= MyFunc(val1) %>
and
<% MyFunc(val1) %>
What is the difference in using the "=" character at the beginning of these sections?
<% evaluates an expression in server code but doesn't emit output.
<%= also evaluates the expression but wraps the result in Response.Write, so it produces output.
When you see:
<%= MyFunc() %>
it really means:
<%
Response.Write( MyFunc() )
%>
Its short hand for writting output to the response.
<%
MyFunc()
%>
The above will just run the code but won't write it to the response unless it has some Response.Write's inside the Function/Sub itself.