I am using a lot of .html.erb partials as "helpers". I'm not sure if this is a really bad habit or not.
main_file.html.erb
render 'shared/some_helper.html.erb',
param_1: param_1,
param_2: param_2,
...,
optional_param_1: optional_param_1,
...
shared/some_helper.html.erb
<%
optional_param_1 ||= optional_param_1_default
optional_param_2 ||= optional_param_2_default
...
# Some additional logic
%>
<!-- The HTML that uses the params -->
I feel this is justified because my helpers are more about HTML than ruby/rails.
But is there actually a better way to do it (that remains as convenient) ? Maybe define some function in applicaton_helper.erb that would output the HTML ? But then I won't be able to write HTML...
Related
Okay I've been searching for this one and found a possible duplicate here (what does " <%: " do?). However this question adresses <%= and <%:, but not <%-.
So just to be sure I'm still asking my question.
I'm trying to setup a node.js/express/d3 application rendered on the server side. I found a repo describing what I want to do here:
https://github.com/gregjopa/d3-server-side-demo/blob/master/index.html
In that html code there is a snippet that I'd like to actually convert in jade:
<h1>D3 Server-side Demo</h1>
<%-
barChartHelper.getBarChart({
data: fixtureData,
width: 400,
height: 300,
xAxisLabel: '2012',
yAxisLabel: 'Views',
containerId: 'bar-chart-small'
})
%>
So I have two questions:
1)What is the meaning of the <%- %> in html?
And
2) How do I write this in jade?
Thanks,
The codebase you linked has EmbeddedJS as a dependency. EJS is a templating library which allows for using those tags.
Unescaped buffering with <%- code %>
So basically: These are not HTML tags, just tags belonging to a different templating language that allow inline JS code to be executed
You can use the same in Jade as explained in the docs
Unescaped Buffered Code
Unescaped buffered code starts with != and outputs the result of evaluating the JavaScript expression in the template.
Essentially I want something like the following:
<code class="snippet">
<%= html_escape do %>
My markup displayed to user
<% end %>
</code>
However the html_escape method does not accept a block. If this is not built into Rails API somewhere else, perhaps using some helper, does anyone have advice on how to make a custom helper where the yield statement output is captured into a string that I can then escape myself?
Thanks,
Keith
Rails' capture and escape_once helper methods can create a String from a block in an erb template and then output an escaped version of it:
<% snippet = capture do %>
My markup displayed to user
<% end %>
<code><%= escape_once snippet %></code>
content_for is another helper that provides similar functionality to capture, that you may consider using depending on the situation.
To explain, snippet is an ActiveSupport::SafeBuffer, and is why escape_once is needed. You could achieve the same by calling snippet.to_str instead of escape_once snippet (However .to_s will not work as that is different to .to_str in ActiveSupport::SafeBuffer).
I've got a method in my view that decides which HTML to print out depending on the class of the object it gets:
<% def laybricks(c,stream) %>
<% if c.is_a?(Post) %>
<article>
// Tons more HTML unique to posts
</article>
<% elsif c.is_a?(Photograph) %>
<article>
// Tons more HTML unique to photographs
</article>
<% end %>
<% end>
But I now know more about Rails since writing it. How can I transfer this to my model so that I can call the method in the view and have it print the HTML the same?
Method in a view would definitely be better moved to the helper.
But for your need the best solution is partials utilizing some rails magic:
<%= render #c %>
will magically render partial _post.html.erb if #c is an instance of Post and _photograph.html.erb if it is instance of Photograph. These partials should be in the same view folder, and you can reference your object as post and photograph in each partial respectively.
For more info see Rails Rendering Guide.
ADDON:
How can I transfer this to my model so that I can call the method in the view and have it print the HTML
It is usually not a responsibility of a model to generate HTML, but if you ultimately want to do this, the best way is to use a design pattern that is usually referred to as Presenter or Decorator. E.g. draper gem exists for this, but also many other. And there is many discussions in Rails community how this pattern should be called, how it should be implemented and is it worth it at all.
UPDATE:
Application Helper
def laybricks(c,stream)
content = []
if c.is_a?(Post)
content << "<article> </article>"
elsif c.is_a?(Photograph)
content << "<article> </article>"
end
end
Your View
<%= raw laybricks %>
OR
<%= laybricks.html_safe %>
NOTE: The raw helper will allow the plain html to get interpreted. You can also use html_safe method to interpret the same.
Should I be using the content_tag helper for all html tags when working with Rails?
Is it The Rails Way to use content_tag for even simple things like Header tags?
<%= content_tag :h2, :class => "bla bla" do %>
Header
<% end %>
vs.
<h2>Header</h2>
Clearly just using straight html is much 'simpler' and 'shorter', but what is the correct Rails Way of doing things?
Using content_tag when you don't have to is a waste. There's no need to use ERBisms to generate static HTML so don't do it. If some other piece of code determines what tag to use, then you'd use content_tag to construct that tag.
If you are asking the rails way of doing this, then its defiantly using 'content_tag', but using tag_helpers has its own advantages and disadvantages
Personally for me I can see these things, (Using rails helpers instead of pure HTML)
Advantages
1 - Your code will be cleaner. (with less lines)
2 - You will have more control other the elements.
Ex: You can have your own helper tags like 'big_text_box' which will return a text box more than the normal with and you can use it across all the site
3 - You will be able to add attributes like class, id dynamically in the runtime
Disadvantages
1 - If you have a separate designer (I mean UI engineer) he/she will get confuse of by the code you have use. As its not pure html
2 - Its slow than the pure html (But this will not even noticeable unless otherwise your app is a damn major one...)
So its up to you to decide what to use, personally I prefer using rails helper tags as it makes me more comfortable
HTH
cheers
sameera
One useful method is the "div_for", which is somewhat similar to the content_tag. If you find yourself marking up HTML elements with data you can reference later, "div_for" makes your life much easier.
Let's say you have a bunch of people being shown on a page and you need to wrap each with a div that has a unique ID so you can modify these elements with JS. By hand and straight HTML you would have to do:
<% #people.each do |p| %>
<div id="person_<%= p.id %>"><%= p.name %></div>
<% end %>
That would get bothersome if you were doing LOTS of this with multiple attributes (I commonly use custom IDs, classes, and some data attributes). But using div_for you could write the above as:
<% #people.each do |p| %>
<%= div_for(p) do %><%= #person.name %><% end %>
<% end %>
Makes the HTML a little easier to read when things get long and complex. I found it is much cleaner when working with javascript a lot.
http://apidock.com/rails/ActionView/Helpers/RecordTagHelper/div_for
I have a lot of comments in Rails views.
How i can prevent rendering them ?
If I understand the question correctly, you're asking about Ruby/Rails comments vs HTML comments... Give this a try in your view:
<!-- This is an HTML comment and will show up in the HTML source! -->
Now try this:
<%# This is a comment that won't show up in the HTML source! %>
<%#
You can even use this for commenting multiple lines!
How useful!
%>
Does that help?
use =begin and =end to mark the beginning and end of your comment
There is no easy way to do that. You can monkey patch ERB sources, perhaps, but it is a bit nerdy.
I'm not a Rails programmer, but a quick but of Binging brought up this link:
http://blog.brijeshshah.com/strip-tags-in-rails-javascript-and-php/
The approach he's using is one that I've used in the past where you sanitize the view's output. sanitize being the name of the function you want to use before rendering the view.
Maybe you can use Haml Comments: -# allow to comment your haml code without them appearing in the generated html.
Kind of hackish, but you can wrap it in a helper method
In your view:
<% comment do %>
<%= "this won't be executed" %>
or displayed
<% end %>
in app/helpers/application_helper.rb
module ApplicationHelper
def comment(&block)
## you can do something with the block if you want
end
end