Ruby-on-rails 在渲染中传递参数 - Rails 3
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11283675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pass Parameters in render - Rails 3
提问by user749798
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?
这与我在渲染中渲染这个事实有什么关系吗?
i.e. 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.
所以我在渲染中渲染它。
回答by Someth Victory
try this:
尝试这个:
<%= render :partial => '/memory_books/new', :locals => {:fbookupload => "yes"} %>
回答by Vadym Tyemirov
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, e.g.
但是如果有一个引用来渲染相同的部分而不指定局部变量,例如
render '/memory_books/new'
then fbookuploadvariable becomes unavailable. The same applies to multiple local variables, e.g.
然后fbookupload变量变得不可用。这同样适用于多个局部变量,例如
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 var2will become unavailable. Go figure ...
然后var2将变得不可用。去搞清楚 ...
回答by Yuri Barbashov
Params is just request parameter, so if u want to pass it in params u have to add it to your url ?fbookupload=yesor 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 withparams[:fbookupload] || fbookupload', and pass fbookupload in locals hash for partial.
但是如果你需要使用params[:fbookupload]', u can replace it withparams[:fbookupload] || fbookupload',并在局部哈希中传递 fbookupload。
回答by Anil
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". Then it is available everywhere. The partial will then be : <%= @fbookupload %>
理想情况下,在控制器中,否则在视图中,定义一个实例变量:@fbookupload = "yes"。然后它无处不在。部分将是:<%= @fbookupload %>
回答by estani
Already answered but to confirm this works with rails 5.2:
已经回答,但要确认这适用于 rails 5.2:
partial call:
部分调用:
<%= render partial: 'some_partial', locals: { value: 'some_value' } %>
You need to explicitly add partial, otherwise it won't work.
您需要明确添加partial,否则将无法正常工作。
In the partial itself you access as a local variableso this holds: value == 'somevalue'.
在部分本身中,您可以作为 a 访问,local variable因此它成立:value == 'somevalue'。
Check rails documentationregarding partial renderers.
检查有关部分渲染器的rails文档。

