rails 3 render partial with params

user1214966 picture user1214966 · Mar 22, 2012 · Viewed 31.4k times · Source

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.

Answer

Thilo picture Thilo · Mar 22, 2012

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