12

I'm having a problem passing some parameters to a partial. No matter what I've tried the params don't pass when the partial is rendered.

I am using a jquery tabbed layout, and each tab displays work orders in a particular status and also based on a range of dates.

I am using the params :sort_filter and :status_filter to accomplish this.

My original code is here, but I want to change this to render partials in the link_to's instead of the way it's listed here:

         <ul>
            <li><%= link_to "Active", work_orders_path(params.merge({:status_filter => "A", :sort_filter => params[:sort_filter]}))  %></li>
            <li><%= link_to "On Hold", work_orders_path(params.merge({:status_filter => "H", :sort_filter => params[:sort_filter]}))  %></li>
            <li><%= link_to "Completed", work_orders_path(params.merge({:status_filter => "C", :sort_filter => params[:sort_filter]}))  %></li>
            <li><%= link_to "Billed", work_orders_path(params.merge({:status_filter => "B", :sort_filter => params[:sort_filter]}))  %></li>
        <li><%= link_to "All", work_orders_path(params.merge({:status_filter => "E", :sort_filter => params[:sort_filter]})) %></li>
     </ul>

So instead of linking to the index in my work_orders_path, I'd like to link to a partial called viewall. Like this:

<%= link_to render (:partial => 'viewall', :status_filter => "E", :sort_filter => params[:sort_filter]) %>

I need to be able to pass the merged parameters with the partial if possible. I've looked at all the documentation and googled my fingers off yesterday all day for an answer to this and still came up with nothing. Any help would be appreciated.

user1214966
  • 273
  • 1
  • 4
  • 10

1 Answers1

31

The syntax to pass a variable @foo to a partial is this:

render :partial => "partial", :locals => { :foo => @foo }

Then it is available in the partial as foo.

See section 3.4.4 of this guide.

EDIT: Since Rails 3.?.?, a more concise version is this:

render "partial", foo: @foo
Thilo
  • 17,565
  • 5
  • 68
  • 84
  • Actually I tried that and it didn't pass the variable at all. render(:partial => "viewall", :locals => { :status_filter => "H" }) - tried it with and without the (). Also trying without success to get this to work with a link_to. Not sure if it's possible or not. – user1214966 Mar 22 '12 at 17:58
  • 1
    Make sure you access it as `status_filter` in the partial, not `@status_filter` or `params[:status_filter]` – Thilo Mar 22 '12 at 18:02
  • I have a general question just to make sure I'm doing this right. Do partials hit the controller before they load? I'm assuming they do (render), but if they don't I'm going to have a problem because my controller uses these params to select rows from my work order table based on date ranges and statuses. My intention in all of this would be to have work orders appear in different tabs based on what status they're in. – user1214966 Mar 22 '12 at 19:40
  • Controllers render views, which in turn can include partials. The guide I linked is a great introduction how it all fits together. – Thilo Mar 22 '12 at 19:53
  • 2
    also: `render "partial", {:foo => @foo}` – tokland Jul 04 '12 at 09:27