Passing parameters to render - Rails 3

I saw a couple of questions on this issue, but could not solve them ...

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

I'm using this line right now:

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

and in part I tried to get the contents of fbookupload with:

 <%= fbookupload %> 

which throws an error "undefined local variable" and

 <%= params.inspect %> 

which does not show fbookupload as a parameter.

How to get partial pass by parameter: fbookupload?

Thanks.

UPDATE:

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

i.e. page (/ fbookphotos / show) with

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

displayed by another page with (messages / shows) via:

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

so I'm rendering this in a render.

+10
source share
5 answers

try the following:

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

Conclusion from comments for posterity. This syntax is correct:

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

But if there is a link to rendering the same particle without specifying local variables, for example

 render '/memory_books/new' 

then the fbookupload variable becomes unavailable. The same applies to several local variables, for example.

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

will work if it happens only once. But if there is something else in the code in the code

 render 'my_partial', var1: 'qq' 

then var2 will become unavailable. Go figure ...

+14
source

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

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

+1
source

To do it your own way:

In the main view:

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

And in partial:

 <%= fbookupload %> 

Second option:

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

0
source

Already answered, but to confirm this, it works with rails 5.2:

partial call:

 <%= render partial: 'some_partial', locals: { value: 'some_value' } %> 

You need to explicitly add partial , otherwise it will not work.

In the most part, you get access as a local variable , so this is the case: value == 'somevalue' .

Check the rails of the partial renderer documentation .

0
source

All Articles