Nested capture_haml helpers - ruby-on-rails-3

I have the following two helper methods:
def hello
capture_haml do
haml_tag :div, 'Hello'
end
end
def hello_world
capture_haml do
hello # How can I call it here?
haml_tag :div, 'World'
end
end
I want to call hello in hello_world. I tried hello alone, capture_haml hello and haml_tag hello, combined with .html_safe also but no solution would ever work.
How can I do that?
I'd really rather use capture_haml than haml_tag straight because I think having in the view
= hello_world
is much cleaner than
- hello_world
Thanks

Your hello method, since it is using capture_haml simply returns a string. When you call it inside the capture_haml block in the hello_world method it doesn’t do anything — a string is created and returned but you don’t use it at all. Since it isn’t written to the output it isn’t captured by the capture_haml.
You can write the string to the output, which will force capture_haml to work, with haml_concat, which would look like this:
def hello_world
capture_haml do
haml_concat hello
haml_tag :div, 'World'
end
end
This is a pretty contrived example, but I hope it shows what’s happening. capture_haml takes a block that would normally be written directly to the output (usually Haml source) and returns it as a string instead. haml_concat takes a string and writes it to the output, so is in some ways the opposite of capture_haml.

Found it:
def hello
capture_haml do
haml_tag :div, 'Hello'
end
end
def hello_world
capture_haml do
haml_tag :div, hello
haml_tag :div, 'World'
end
end

Related

Why is my instance not saving for my search form?

I've got a search form that returns our products. However, if a user inputs a string that contains certain words (in this instance, 'color'), it returns far too many products. I'm trying to remove the string 'color' from the query that is searched on the backend, but maintain the original query's string as #unfiltered_query so I can reference the #unfiltered_query on the front-end template.
if query.include? "color"
#unfiltered_query = query
end
query.slice! "color"
values = query.split
binding.pry
It was not working, so I ran pry to see what was going on. In the form, I searched "Red paint color". When I call #unfiltered_query in pry, it outputs "Red paint", even though I create the method before .slice! is called?!
What am I missing?
Thank you!
p.s. the HTML template that I'm using to reference the instance is:
<div class="search-input"><h2>
<% if #unfiltered_query.present? %>
<%= #unfiltered_query.titleize %>
<% else %>
<%= query.titlelize %>
<% end %>
</h2></div>
Can you try like this :
if query.include? "color"
#unfiltered_query = query.dup
end
query.slice! "color"
values = query.split
binding.pry
This could be due to passing by reference.

How to integrate HTML with ROR

I am new in ROR
I want integrate html code with ROR Code Like PHP
This is php Exmple code:-
$output='';
$output.='<div>how r you?</div>';
$output.='<div> fine</div>';
echo $output;
Output
how r you? fine
Help how to integrate html with ROR code? Like php see above the php code.
Use String#+, it's like PHP's . String operator:
output = "Hello"
output += " world!" # same as output = output + " world!"
Or use String#<<
output = "Hello"
output << " world!"
Which is the same as String#concat:
output = "Hello"
output.concat(" world")
The last two examples modify the contents of output.
You can use helpers to integrate html code with the ROR code.
In the helpers itself you can generate tags as <p> or <div> and close them after inserting the data from the back end
In short you can generate html pages dynamically in the helpers. Helpers are particularly for views
You can refer to the below link for more clarity
Using helpers in rails 3 to output html
In rails there is a available method called html_safe(). You can use this to integrate html code in .html.erb file
controller:-
#test = ("<h1>Test case</h1>").html_safe
html.erb:-
<%= #test %>
Try below code to print html on page:
Controller
#data = "<h1>Testing...</h1>"
#data=+ "<h2>Hello</h2>"
view
<%= #data.html_safe %>
OR
<%= raw(#data) %>
Controller
#data = "<h1>Testing...</h1>"
#data=+ "<h2>Hello</h2>"
Views
<%= #data.html_safe%>

Use translation for submit button

I don't want to use the default
<%= f.submit %>
and have created a helper function for it, which also embeds an icon. The helper function expects a label to put on the newly created button.
I'm calling it like this:
<%= submit_button("icon-plus", I18n.translate("helpers.submit.create")) %>
But now on this text appears on the button:
%{model} toevoegen
Instead of:
Product type toevoegen
If I use the normal submit button then the correct text appears so my yml files are correct. How can I get the correct text to use in the helper?
Helper code:
def submit_button(icon, label)
link_to "javascript:void(0)", :class => 'btn btn-primary', :onclick => "$(this).closest('form').submit()" do
raw('<div class="') + icon + raw(' icon-white"> ') + label +raw('</div>')
end
end
As the I18n guide says, the translate function interpolates variables passed in the %{} brackets using its second argument (a hash).
In your case you need to tell it the model by doing this:
I18n.t("helpers.submit.create", model: "Product type")
If you want a generic option that would work for any model, you can see how Rails itself does it by looking at the source on GitHub - it's something like
I18n.t("helpers.submit.create", model: f.object.class.model_name.human)
As an aside, you don't need to (and probably shouldn't) use raw there. What you are trying to achieve could easily be done with the built-in helpers:
link_to ... do
content_tag :div, label, class: "#{icon} icon-white"
end

How to get the current view name from layout template in Rails?

Is it possible to get the name of the currently rendered view from inside layout?
I did something like this for css namespacing:
# config/initializers/action_view.rb
ActionView::TemplateRenderer.class_eval do
def render_template_with_tracking(template, layout_name = nil, locals = {})
# with this gsub, we convert a file path /folder1/folder2/subfolder/filename.html.erb to subfolder-filename
#view.instance_variable_set(:#_rendered_template, template.inspect.gsub(/(\..*)/, '').split('/')[-2..-1].join('-'))
out = render_template_without_tracking(template, layout_name, locals)
#view.instance_variable_set(:#_rendered_template, nil)
return out
end
alias_method_chain :render_template, :tracking
end
# application.html.erb
<body class="<%= :#_rendered_template %>" >
Use <% __FILE__ %> to get the complete file path of current view, but you can only use it from within the file itself without writing some helpers
The method active_template_virtual_path method returns the template as a name in the following form "controller/action"
class ActionController::Base
attr_accessor :active_template
def active_template_virtual_path
self.active_template.virtual_path if self.active_template
end
end
class ActionView::TemplateRenderer
alias_method :_render_template_original, :render_template
def render_template(template, layout_name = nil, locals = {})
#view.controller.active_template = template if #view.controller
result = _render_template_original( template, layout_name, locals)
#view.controller.active_template = nil if #view.controller
return result
end
end
I had a similar question. I found <%= params[:controller] %> and <%= params[:action] %> to suit my need, which was to add the controller name and action name as classes on the body tag.
Just in case that helps anyone. :)
I'm currently using a modified version of Peter Ehrlich's solution. The resulting string is of the form controller_name/view_name, e.g. users/new, which means it can be passed directly to render later on, or altered to suit other uses. I've only tried this with Rails 4.2, though as far as I know it ought to work all the way back into the 3.xes.
ActionView::Base.class_eval do
attr_accessor :current_template
end
ActionView::TemplateRenderer.class_eval do
def render_template_with_current_template_accessor(template, layout_name = nil, locals = {})
#view.current_template = template.try(:virtual_path)
render_template_without_current_template_accessor(template, layout_name, locals)
end
alias_method_chain :render_template, :current_template_accessor
end
For debugging purpose, you can use gem 'current_template' from here.
This gem inspects logfile and display file name of view/partial template.
For example:
Also, you can simply add this line
<%= "#{`tail log/development.log`}".scan(/\s[a-z]+\/\S+/) %>
to your layout/application.html.erb.

Helper function not returning string

So I'd like to generate a random background-color based on an array:
def panel_color
a = ["#E5E0AE","#A4D349","#F1427B","#F09137","#792060"]
return a.sample
end
Simple enough. This is to be used in my disc#index.erb view, so I call it there:
...
<div class="panel" style="background-color: <% panel_color %>;">
...
Since this is a helper method for the view, I placed the function in helpers/disc_helper.rb
module DiscHelper
def panel_color
a = ["#E5E0AE","#A4D349","#F1427B","#F09137","#792060"]
return a.sample
end
end
Which, to my surprise, returns nothing to the view, but does not error, either. I'm thinking I missed something very obvious here, but I'm not quite sure what. Latest rails here.
You're just executing it, not displaying it. Use <%= ... %> instead:
<%= panel_color %>
def panel_color
["#E5E0AE","#A4D349","#F1427B","#F09137","#792060"].sample
end