Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can 'locals' be used with 'collection' when rendering partials in Rails?

Everything works okay when I try to render a partial like this:

= render :partial => "/shared/enquiry/car_type", :collection => @enquiry.available_car_types

However, if I also want to pass a variable (in this case 'path', because I'm sharing this partial across two forms), the path is not available to me:

= render :partial => "/shared/enquiry/car_type", :collection => @enquiry.available_car_types, :locals => {:path => customers_enquiry_path}

I've tried moving things around, but nothing appears to work, leading me to believe one cannot use locals with collections. Any help would be appreciated.

Gav

like image 746
Gav Avatar asked Jun 01 '10 09:06

Gav


3 Answers

Like the guides said, specify the :as option in the call to the partial

enter image description here

like image 119
xguox Avatar answered Nov 08 '22 16:11

xguox


For Rails 4.x, if you pass the collection directly to render (which you must do if you want to use automatic partial selection for a heterogeneous collection), the next parameter is interpreted as a hash of locals.

Try the following:

= render @enquiry.available_car_types, :path => customers_enquiry_path
like image 19
seren23 Avatar answered Nov 08 '22 15:11

seren23


What version are you using? Using my 2.3.5 I'm able to do just that:

render :partial => "/site_articles/article", :collection => @site_articles, :locals => { :footer => true }

, which you can find explained in other places, such as 3.4.6 of this.

like image 12
par Avatar answered Nov 08 '22 17:11

par