user749798
user749798

Reputation: 5370

Pass Parameters in render - Rails 3

I've seen a couple questions on this but haven't been able to solve it...

I'm trying to pass a parameter while rendering a partial (similar to domainname.com/memory_books/new?fbookupload=yes)

Right now, I use this line:

<%= render :partial => '/memory_books/new', :fbookupload => "yes" %>

and in the partial, I have tried to get the content of fbookupload by using:

<%= fbookupload %>

which gives an "undefined local variable" error and

<%= params.inspect %>

which does not show fbookupload as a parameter.

How can I have the partial pass along the parameter :fbookupload?

Thank you.

UPDATE:

Could it have anything to do with the fact that I'm rendering this within a render?

i.e. the page (/fbookphotos/show) that has

<%= render :partial => '/memory_books/new', :fbookupload => "yes" %>

is being rendered by another page with (posts/show) via:

<%= render :partial => '/fbookphotos/show' %>

so I'm rendering this within a render.

Upvotes: 15

Views: 58799

Answers (5)

stephen.hanson
stephen.hanson

Reputation: 9604

render can be called with or without the partial param, and there seems to be some confusion around the differences between these two forms.

The following two are equivalent:

<%= render "my_partial', my_param: true %>

and:

<%= render partial: "my_partial', locals: { my_param: true } %>

The first is a shorthand that allows you to omit partial:. With this shorthand, local variables are also not nested under locals:. This is explained well in the documentation (see 'Rendering the default case').

In the two cases above, you would access my_param in the partial directly with my_param.

One other source of confusion is that if you render the partial somewhere without passing my_param, then the partial will fail when it tries to access it. To get around this, you can access the local with local_assigns[:my_param] instead of my_param, which will give you nil if the param is not defined instead of erroring, as described in this documentation. Another alternative is to use defined?(my_param) before accessing it.

Upvotes: 2

Vadym Tyemirov
Vadym Tyemirov

Reputation: 8833

Taking it out of the comments for posterity. This syntax is correct:

render '/memory_books/new', fbookupload: "yes"

But if there is a reference to rendering the same partial without specifying the local variables, e.g.

render '/memory_books/new'

then fbookupload variable becomes unavailable. The same applies to multiple local variables, e.g.

render 'my_partial', var1: 'qq', var2: 'qqq'

will work if only occurs once. But if there is something like that somewhere else in the code

render 'my_partial', var1: 'qq'

then the var2 will become unavailable. Go figure ...

Upvotes: 18

Yuri  Barbashov
Yuri Barbashov

Reputation: 5437

Params is just request parameter, so if u want to pass it in params u have to add it to your url ?fbookupload=yes or assign it params[:fbookupload] = "yes", but i don't think that is a good idea.

But if u need to use params[:fbookupload]', u can replace it withparams[:fbookupload] || fbookupload', and pass fbookupload in locals hash for partial.

Upvotes: 1

Anil
Anil

Reputation: 3919

To do it your way:

In the main view:

<% fbookupload = "yes" %>
<%= render :partial => '/memory_books/new', :locals => {:fbookupload => fbookupload} %>

And in the partial:

<%= fbookupload %>

2nd option:

Ideally in the controller, otherwise in the view, define an instance variable: @fbookupload = "yes". Then it is available everywhere. The partial will then be : <%= @fbookupload %>

Upvotes: 1

Someth Victory
Someth Victory

Reputation: 4549

try this:

<%= render :partial => '/memory_books/new', :locals => {:fbookupload => "yes"} %>

Upvotes: 20

Related Questions