Reputation: 9681
I am trying to render a partial from the controller (ajax request to controller which renders partial so JS can insert into DOM).
Code for the partial is simple. It requires an object
View
= render "partials/foo", object: @bar
Partial
= object.name
This works fine from the view. But when I try this from the controller (passing params
in as the object) I get the error undefined variable object
Controller
render "partials/foo", object: params[:data]
params[:data]
is just a hash
Upvotes: 0
Views: 1521
Reputation: 10769
Be careful with :object
, as it will expect a variable with the same name as the partial (minus the underscore). You can rename the variable using the :as
option.
if you set render partial: "foo", object: @foo
you will have a variable foo
in the partial.
It is the same as render partial: "foo", locals: { foo: @foo }
You can use :locals
, so you can set a hash of objects in your partial. Assuming that params[:data]
is a hash:
params[:data] = {a: 'something', b: 'another thing'}
render "partials/foo", locals: params[:data]
In your partial you will be able to use the variables a
and b
.
Or, you can define your own variable if params[:data]
has a simple value:
params[:data] = 'something'
render "partials/foo", my_variable: params[:data]
In your partial you will be able to use my_variable
.
Upvotes: 2