rails slim very simple questions - ruby-on-rails

I'm new to slim and there are little things that I don't understand and I don't find answers in the documentation.
linebreak - How can I add this at the end of a line? For example:
<%= name %><br/>
<%= address %><br/>
How can I combine pure html and ruby on the same line? For example:
<p>New building <% if building.ownver %> for <%= owner %><% end %></p>
I know, I must have missed something but there is no real tutorial out there.
BTW, There is no emulator to convert erb to slim?
Thanks.

You can use this converter html to slim here's a link!

The documentation covers this here: https://github.com/slim-template/slim#inline-html--html-style
The example it gives is:
<html>
head
title Example
<body>
- if articles.empty?
- else
table
- articles.each do |a|
<tr><td>#{a.name}</td><td>#{a.description}</td></tr>
</body>
</html>
I don't know if there's an equivalent to the erb <%= "for #{owner}" if building.ownver %> in slim, so I would just use the above information for your second question, as well, by changing the code to:
- if building.ownver
<p>New building</p>
- else
<p>New building for #{owner}</p>

Related

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.

Link_to a page - Ruby on Rails

I have the following code:
<% slika = Refinery::Page.find('sladoledi') %>
<%= link_to (image_tag slika.key_image.url, slika) %>
The problem is that it's not linking to slika. Any suggestions?
Try this format
<%= link_to(slika) do %>
<%= image_tag(slika.key_image.url)%>
<% end %>
also have a look at documentation there are nice examples how to use link_to() helper
http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to
First start nesting code you write here. People have problem reading code like this :(
If you want add code click code button on editor.
I suppose the problem is you don't end image_tag.
Correct form is:
link_to(image_tag(slika.key_image.url),slika)

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.

Best way to add comments in erb

How do we add comments in erb files, if we do not want them to be generated into the html content?
Use the <%# %> sequence, e.g.
<%# This is a great comment! %>
For Record
<%# This is a great comment! %>
<%#= This is a great comment! %>
For block comments:
<% if false %>
code to be commented out...
<% end %>
I have a Windows setup, and this <%-# %> sequence is the only one that works for me:
Example:
<%-# This is a sample comment! %>
In my text editor, i run command + / (sublime-text shortcut). It will be like this.
<%
=begin%>
Here is the comment
<%
=end%>
It doesn't look simply, but it works.
Since .erb is by definition "embedded ruby", you can embed every ruby code between: <%= and the other: %>, typically all written in one line. In addition, ruby one-line comments start always with #, so the <%=# Comment %> style matches perfectly with both pure-ruby and erb styles for one-line comments.
I doesn't work in the Controllers files, I had to put it between slashes
/ comment here.... /

In RoR, is there an easy way to prevent the view from outputting <p> tags?

I'm new to Ruby and Rails and I have a simple controller that shows an item from the database in a default view. When it is displaying in HTML it is outputting <p> tags along with the text content. Is there a way to prevent this from happening? I suppose if there isn't, is there at least a way to set the default css class for the same output in a statement such as this:
<% #Items.each do |i| %>
<%= i.itemname %>
<div class="menu_body">
Link-1
</div>
<% end %>
So the problem is with the <%= i.itemname %> part. Is there a way to stop it from wrapping it in its own <p> tags? Or set the css class for the output?
Thanks!
You need to enclose it with the HTML tag of your choice. Also if required you can escape bad code by using <%=h i.itemname %> Example:
<% #Items.each do |i| %>
<div><%=h i.itemname %></div>
<div class="menu_body">
Link-1
</div>
<% end %>
Edit: Ryan Bigg is right. Rails doesn't output a <p> tag. Sorry for the wrong info.
You canchange the public/stylesheets/scaffold.css if you want.
Or if you want to change it for a single page say items/index.html.erb
<style>
p{
/* your style here *?
}
</style>

Resources