Ruby on Rails: Including multiple instance variables in partials - ruby-on-rails

So... I know about the :locals => { :var1 => #rawr, :var2 => #hello } syntax for partials
but is there a way for me to pass both #rawr and #hello to the partial, so that i don't need to use var1 and var2?

You can do :locals => { :rawr => #rawr, :hello => #hello } and then the variables will be available within the partial as rawr and hello.

You know, you could just use #rawr and #hello ... and NOT pass any variables.

Related

Rails partial - local variable name from variable?

I currently have some code to render a partial, which includes:
:locals => {:feature => y}
This works fine, but I would like to be able to use a variable in place of :feature - I have tried the following:
:locals => {type.singularize => y}
but to no avail. What is the correct syntax to replace :feature with a local variable?
Incase anyone else comes across this, I was able to resolve it using the code in the comments from apneadiving and MurifoX:
:locals => {type.singularize.to_sym => y}

Rails render html into a hash

Is it possible to have rails render html into a hash instead of to the client?
Something like this:
#obj = {
"foo" => Bar.find(1)
"html" => (render :partial => "yatzhee")
}
render :json => #obj.to_json
render_to_string takes all the same arguments as render but returns a string. You can then put that in a hash or do anything you want with that.
I never saw anything like that.
If you want to render json, you should really take a look at jbuilder. It's a joy to work with and it's the 'rails way' (~ it's merely built-in).
It allows you to render partials, for instance:
json.partial! "api/comments/comments", #message.comments

Passing locales to javascript partial in haml

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}"

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 pass variables to render_to_string?

Trying to do the following
#message = render_to_string ( :sender => sender, :template => "template" )
But when accessing #sender in template it turns out to be nil:NilClass. Double checked if I pass the right variable and it's totally fine. Maybe there are other way to pass variables to render_to_string?
It might be the syntax you're using. Try using the :locals argument:
#m = render_to_string :template => "template", :locals => {:sender => sender}
Then you just need to access sender (without an #) as a local variable inside the template.
Here's Jason Kim's solution he wrote in a comment which worked for me:
ActionController::Base.new.render_to_string(
"user_mailer/welcome_email.html.erb", locals: { :#user => user}
)
Please mind the :#user => value bit.
In Rails 5 (atm in beta):
ApplicationController.render(
file: 'path',
assigns: { foo: 'bar' }
)
More here
Try this:
ac = ActionController::Base.new()
ac.render_to_string(:partial => 'path to your partial',:locals => {:varable => your variables})
In rails 4.0.2 this worked:
render_to_string(partial: 'path/to/partial', locals: { argument: 'value'}
I was trying to render a different format of partial in render_to_string. The thing which really worked for me was:
render_to_string(:partial => 'partial_file.html', :locals => {:variable => variable}, :format => :html)
where the name of the file was _partial_file.html.erb.

Resources