Ruby-on-rails 将@object 传递给 rails 部分渲染
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1850905/
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
Passing @object into a rails partial render
提问by David C
I have a partial:
我有一个部分:
'profiles/_show.html.erb'
'个人资料/_show.html.erb'
that contains code like
包含像这样的代码
<%= @profile.fullname %>
I'm trying to render the partial but I'm not sure how to pass the @profile. I tried using local but apparently it sets 'profile' on my partial instead of '@profile'.
我正在尝试渲染部分,但我不确定如何传递@profile。我尝试使用本地,但显然它在我的部分而不是“@profile”上设置了“profile”。
<%= render :partial => 'profiles/show', :locals => {:profile => @app.profile} %>
Is there anyway to pass it as @object instead of object, or is it designed this way?
无论如何将它作为@object 而不是 object 传递,还是这样设计的?
回答by EmFi
Why is it so important that you use an instance variable(variables who's names begin with '@', eg: @object) in your partial? It's not a good habit to get into. Using instance variables in partials complicates the control flow, which facilitates bugs and makes reuse of partials more difficult. This blog postexplains the problem a little more in depth.
为什么在部分中使用实例变量(名称以“@”开头的变量,例如:@object)如此重要?进入并不是一个好习惯。在局部变量中使用实例变量会使控制流程复杂化,这会导致错误的产生并使局部变量的重用更加困难。这篇博文更深入地解释了这个问题。
Really you have two options. The first option is the suggested solution.
真的,你有两个选择。第一个选项是建议的解决方案。
Change all instance variables to a local variable and pass it to the partial with the locals argument of render.
Set the instance variable before the partial is rendered. Partials have access to all the instance variables that your controller sets.
将所有实例变量更改为局部变量,并使用 render 的 locals 参数将其传递给局部变量。
在呈现部分之前设置实例变量。Partials 可以访问您的控制器设置的所有实例变量。
Again instance variables in partials are bad. You should never set instance variables just because your partials are already written to use them. Rewrite the partial instead.
部分中的实例变量再次不好。你永远不应该仅仅因为你的部分已经被编写来使用它们而设置实例变量。改写部分。
回答by Javier Cadiz
Indeed, quoting thisblog post you should know:
事实上,引用这篇博文你应该知道:
A partial is a reusableview template, it allow you to modularize the components which make up a particular page into logical, cohesive pieces. When required data is not passed into a partial, it is often difficult to reuse or change later.
部分是一个可重用的视图模板,它允许您将组成特定页面的组件模块化为逻辑的、有凝聚力的部分。当所需的数据没有传递到部分时,通常很难在以后重用或更改。
The rails guidessite explains some simple use cases:
该导轨导向的网站介绍一些简单的使用案例:
You can also pass local variables into partials, making them even more powerful and flexible. For example, you can use this technique to reduce duplication between new and edit pages, while still keeping a bit of distinct content ...
您还可以将局部变量传递给局部变量,使它们更加强大和灵活。例如,您可以使用这种技术来减少新页面和编辑页面之间的重复,同时仍然保留一些不同的内容......
So in your specific case there is a simple and powerful way to do that
因此,在您的特定情况下,有一种简单而强大的方法可以做到这一点
<%= render partial: 'show', locals: {profile: @profile} %>
Extra tip:
Probably a partial named show is not a good option, give it a more meaningful name related to the thing you are trying to reuse. This sounds even more error/confusion prone in a RESTful scenario when you actually have a showmethod, so probably would be a _show.html.erbfile and a show.html.erbfile.
额外提示:
可能部分命名的节目不是一个好的选择,给它一个与您尝试重用的东西相关的更有意义的名称。当您实际上有一个show方法时,这在 RESTful 场景中听起来更容易出错/混淆,所以可能是一个_show.html.erb文件和一个show.html.erb文件。
Hope this helped.
希望这有帮助。
回答by Grant Birchmeier
For Rails 3+, the answer to your actual question is:
对于 Rails 3+,您实际问题的答案是:
<%= render 'profiles/show', :@profile => blah %>
Your partial will now see @profilelocally.
您的部分现在将@profile在本地看到。
I'm not saying this is a good approach. I'm honestly not sure if this is ugly or not. But it works.
我并不是说这是一个好方法。老实说,我不确定这是否丑陋。但它有效。
回答by Jim
You could always do @profile = profilein the partial.
你总是可以做@profile = profile部分。
回答by Ahmad Ramdani
this is more simple
这更简单
<%= render :partial => "profiles/show", :collection => @profiles %>
on partial _show.html.erb
在部分 _show.html.erb 上
<%= profile.fullname %>
hope helped
希望有所帮助

