简体   繁体   English

在渲染中传递参数 - Rails 3

[英]Pass Parameters in render - Rails 3

I've seen a couple questions on this but haven't been able to solve it...我已经看到了一些关于此的问题,但一直无法解决...

I'm trying to pass a parameter while rendering a partial (similar to domainname.com/memory_books/new?fbookupload=yes)我试图在渲染部分时传递一个参数(类似于 domainname.com/memory_books/new?fbookupload=yes)

Right now, I use this line:现在,我使用这一行:

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

and in the partial, I have tried to get the content of fbookupload by using:在部分中,我尝试使用以下方法获取 fbookupload 的内容:

<%= fbookupload %>

which gives an "undefined local variable" error and这给出了“未定义的局部变量”错误和

<%= params.inspect %>

which does not show fbookupload as a parameter.它不显示 fbookupload 作为参数。

How can I have the partial pass along the parameter :fbookupload?我怎样才能部分传递参数:fbookupload?

Thank you.谢谢你。

UPDATE:更新:

Could it have anything to do with the fact that I'm rendering this within a render?这与我在渲染中渲染这个事实有什么关系吗?

ie the page (/fbookphotos/show) that has即页面 (/fbookphotos/show)

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

is being rendered by another page with (posts/show) via:正在由另一个页面呈现(帖子/显示)通过:

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

so I'm rendering this within a render.所以我在渲染中渲染它。

试试这个:

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

Taking it out of the comments for posterity.把它从后人的评论中取出来。 This syntax is correct:这个语法是正确的:

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

But if there is a reference to rendering the same partial without specifying the local variables, eg但是如果有一个引用来渲染相同的部分而不指定局部变量,例如

render '/memory_books/new'

then fbookupload variable becomes unavailable.然后fbookupload变量变得不可用。 The same applies to multiple local variables, eg这同样适用于多个局部变量,例如

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

will work if only occurs once.如果只发生一次就会起作用。 But if there is something like that somewhere else in the code但是如果代码中的其他地方有类似的东西

render 'my_partial', var1: 'qq'

then the var2 will become unavailable.那么var2将变得不可用。 Go figure ...去搞清楚 ...

Params is just request parameter, so if u want to pass it in params u have to add it to your url ?fbookupload=yes or assign it params[:fbookupload] = "yes" , but i don't think that is a good idea. params 只是请求参数,所以如果你想在 params 中传递它,你必须将它添加到你的 url ?fbookupload=yes或分配它params[:fbookupload] = "yes" ,但我认为这不是一个好主意。

But if u need to use params[:fbookupload]', u can replace it with params[:fbookupload] ||但是如果你需要使用params[:fbookupload]', u can replace it with params[:fbookupload] || params[:fbookupload]', u can replace it with fbookupload', and pass fbookupload in locals hash for partial. fbookupload',并在局部哈希中传递 fbookupload。

render can be called with or without the partial param, and there seems to be some confusion around the differences between these two forms.可以使用或不使用partial参数来调用render ,并且这两种形式之间的差异似乎有些混乱。

The following two are equivalent:下面两个是等价的:

<%= render "my_partial', my_param: true %>

and:和:

<%= render partial: "my_partial', locals: { my_param: true } %>

The first is a shorthand that allows you to omit partial: .第一个是允许您省略partial:的速记。 With this shorthand, local variables are also not nested under locals: .使用这个速记,局部变量也不会嵌套在locals: This is explained well in the documentation (see 'Rendering the default case').这在文档中有很好解释(参见“渲染默认情况”)。

In the two cases above, you would access my_param in the partial directly with my_param .在上述两种情况下,你将访问my_param在部分直接与my_param

One other source of confusion is that if you render the partial somewhere without passing my_param , then the partial will fail when it tries to access it.另一个令人困惑的来源是,如果您在某处渲染部分而不传递my_param ,则部分在尝试访问它时将失败。 To get around this, you can access the local with local_assigns[:my_param] instead of my_param , which will give you nil if the param is not defined instead of erroring, as described in this documentation .为了解决这个问题,您可以使用local_assigns[:my_param]而不是my_param访问本地,如果未定义参数而不是错误,它将为您提供nil ,如本文档中所述 Another alternative is to use defined?(my_param) before accessing it.另一种选择是在访问它之前使用defined?(my_param)

To do it your way:按照你的方式去做:

In the main view:在主视图中:

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

And in the partial:在部分:

<%= fbookupload %>

2nd option:第二个选项:

Ideally in the controller, otherwise in the view, define an instance variable: @fbookupload = "yes" .理想情况下,在控制器中,否则在视图中,定义一个实例变量: @fbookupload = "yes" Then it is available everywhere.然后它无处不在。 The partial will then be : <%= @fbookupload %>部分将是: <%= @fbookupload %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM