传递@object进入轨道部分渲染 [英] Passing @object into a rails partial render

查看:197
本文介绍了传递@object进入轨道部分渲染的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个部分:

配置文件/ _show.html.erb

'profiles/_show.html.erb'

这包含code像

<%= @profile.fullname %>

我试图渲染部分,但我不知道如何通过@profile。我尝试使用本地,但显然它设置在我的部分'个人资料',而不是'@profile。

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'.

<%= render :partial => 'profiles/show', :locals => {:profile => @app.profile} %>

反正是有传递它作为@object而不是目的,抑或是设计成这样?

Is there anyway to pass it as @object instead of object, or is it designed this way?

推荐答案

为什么这么重要的是你使用的实例变量(变量谁的名称以@,如:@object)在你的部分?这不是一个好习惯进入。在谐音使用实例变量的复杂控制流,这有利于漏洞,使谐音的再利用更加困难。该<一href="http://www.robertshowalter.com/articles/2007/07/24/dont-use-instance-variables-in-partials">blog帖子解释该问题多一点深入。

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 post explains the problem a little more in depth.

真的,你有两个选择。第一种选择是所建议的解决方案。

Really you have two options. The first option is the suggested solution.

  1. 将所有实例变量一个局部变量,并把它传递到部分与渲染的当地人的说法。

  1. 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.

同样的实例变量是不好的。你不应该只设置实例变量,因为你的谐音已写入使用它们。重写部分代替。

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.

这篇关于传递@object进入轨道部分渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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