Passing locales to javascript partial in haml - ruby-on-rails

I have a partial that contains javascript
#shared/monkey.js
:javascript
//javascript code here
and I want to be able to use partial in that
render :partial => 'shared/monkey', :locals => {:monkey => 'HELLO'}
how do i use the variable monkey inside my partial?

actually, found out a way.
in order to execute ruby code in your :javascript block, you need to do
"#{ruby_code}"

Related

Partial locals are not being passed

I'm trying to pass some locals into a partial render in rails, however they are not getting passed.
<%=render :partial => "search_results", :locals => {:refinement => "all", :query => params[:search]}%>
The partial is being rendered in a view that has the param[:search] in it, that params[:search] is accessable inside of the partial without it being passed in as a local.
I'm trying to access the locals refinement and query by doing params[:refinement] and params[:query] inside of the partial, however they are not being recognized.
You don't access them via params - you just access the local variable, eg. refinement or query.
Access them as locals[:refinement] or locals[:query] inside your partial. That should work.

Haml nesting with rendered partial in Rails

Consider a partial.haml like this:
- haml_tag :body, {:id => #instance_var}
What I'm trying to do is to nest haml content under a rendered partial. Something like:
= render 'partial_file' do
%h1 My test
%p Trying to nest under a partial output.
But this is causing view errors like:
'nil' is not an ActiveModel-compatible object that returns a valid partial path.
Is there any similar solution to accomplish this?
I solved this per the recommendation in Rails: How to render repetitive form block? by specifying the render as :layout:
= render :layout => 'partial_file' do
%h1 My test

Including a partial "as is" in Ruby on Rails

I am using stache for server-side evaluation of Mustache templates. I would like to re-use some of these templates on the client-side from JavaScript using ICanHaz.js, but to do so I need to include them into script tags. I would like to avoid duplicating the templates (DRY), but obviously, the templates must not be evaluated before being sent to the client, so using a simple render :partial invocation like in this (HAML) snippet does not work:
%script{:id => 'project_snippet'}
= render :partial => 'project'
Is there any way to include a partial without evaluating it using the underlying template engine (kind of like a raw include)?
In other places the partial is to be used as regular partial, i.e., evaluation is supposed to happen, so changing the file extension to always avoid evaluation is not an option.
do you need a partial as is or you want it to be rendered as HTML with some placeholders for JavaScript templating? you can pass :locals => { ... } with something to be replaced by JS template engine later i.e.
%script{:id => 'project_snippet'}
= render :partial => 'project', :locals => {:name => '{{{ project_name }}}'}
if as is then read the partial content (but it doesn't look like you want this)
%script{:id => 'project_snippet'}
= File.open("#{path/to}/partial.html.haml", "r").read
Well, it seems that I should have read the stache documentation: There is a tag helper available, so
= template_include_tag 'projects/project'
will do the trick after setting the template base directory in an initializer:
Stache.configure do |c|
c.template_base_path = "#{Rails.root}/app/views"
end

How to render partial in lib ruby class using render_to_string

I am writing a custom tag for Liquid and want to render a partial in the tag. I am assuming I have to use render_to_string but I can't seem to get it to work.
I've tried all sorts of things, including:
ActionController::Base.render_to_string(:partial => 'path/to/partial')
But nothing seems to work. There has to be some easy way to do this that I'm missing.
Thanks in advance.
The render_to_string method is an instance method:
ActionController::Base.new.send(:render_to_string,
:partial => 'path/to/partial')

ERB in command line with render :partial method in html.erb

I want to render an HTML-EMail and send it to our customers using some ERB Templates.
The basic code I am using:
ERB.new("newsletter.html.erb").result(binding)
doesn't allow me to add partials to the html.erb-File. I would love to move the header and footer to a partial and use the render :partial-Method in that call.
Is this possible? What do I have to add?
This is what I came up with:
viewer = ActionView::Base.new(File.join(Rails::Configuration.new.view_path, "PATH/TO/PARTIALS"))
html = viewer.render(
:file => "PATH/TO/FILE.ERB),
:locals => {:variable => #var}
)
please correct me if there is a more elegant solution than this.

Resources