Rails 3使用params渲染局部 [英] rails 3 render partial with params

查看:131
本文介绍了Rails 3使用params渲染局部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将一些参数传递给局部参数时遇到问题.不管我尝试过什么,渲染部分时参数都不会通过.

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.

我正在使用jquery选项卡式布局,每个选项卡都以特定状态并基于日期范围显示工单.

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.

我正在使用参数:sort_filter和:status_filter来完成此操作.

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

我的原始代码在这里,但我想更改它以在link_to的部分中呈现部分,而不是在这里列出的方式:

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>

因此,我不想链接到我的work_orders_path中的索引,而是想链接到一个名为viewall的局部文件.像这样:

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]) %>

如果可能的话,我需要能够将合并的参数与部分参数一起传递.我查看了所有文档,昨天全天用Google搜索寻找答案,但仍然一无所获.任何帮助将不胜感激.

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.

推荐答案

将变量@foo传递给局部变量的语法是:

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

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

然后它在部分文档中以foo的形式提供.

Then it is available in the partial as foo.

请参见本指南的3.4.4节.

编辑:自Rails 3.?.?起,这是一个更简洁的版本:

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

render "partial", foo: @foo

这篇关于Rails 3使用params渲染局部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆