将@object传递到rails的部分呈现

时间:2022-10-08 13:13:15

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。我尝试使用local,但显然它在我的部分中设置了“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的方式传递它,或者它是这样设计的?

5 个解决方案

#1


32  

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.

为什么在部分中使用实例变量(名称以“@”开头的变量,例如:@object)如此重要?这不是一个好习惯。在部分中使用实例变量会使控制流变得复杂,从而促进bug,并使部分的重用变得更加困难。这篇博文更深入地解释了这个问题。

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

你有两个选择。第一个选项是建议的解决方案。

  1. Change all instance variables to a local variable and pass it to the partial with the locals argument of render.

    将所有实例变量更改为局部变量,并将其传递给带局部变量的render。

  2. Set the instance variable before the partial is rendered. Partials have access to all the instance variables that your controller sets.

    在呈现局部变量之前设置实例变量。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.

部分中的实例变量是不好的。您不应该仅仅因为已经编写了部分来使用实例变量就设置实例变量。重写的部分。

#2


14  

Indeed, quoting this blog post you should know:

事实上,引用这篇博文你应该知道:

A partial is a reusable view 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 guides site explains some simple use cases:

rails指南站点解释了一些简单的用例:

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 show method, so probably would be a _show.html.erb file and a show.html.erb file.

额外提示:可能局部命名的show不是一个好的选择,给它一个与您要重用的东西相关的更有意义的名称。在一个RESTful场景中,当您有一个show方法时,这听起来更容易出错/混淆,所以可能是一个_show.html。erb文件和一个show.html。erb文件。

Hope this helped.

希望这个有帮助。

#3


4  

For Rails 3+, the answer to your actual question is:

对于Rails 3+,您实际问题的答案是:

<%= render 'profiles/show', :@profile => blah %>

Your partial will now see @profile locally.

您的部分现在将在本地看到@profile。

I'm not saying this is a good approach. I'm honestly not sure if this is ugly or not. But it works.

我不是说这是一个好方法。老实说,我不确定这是否丑陋。但它的工作原理。

#4


1  

You could always do @profile = profile in the partial.

可以在部分中使用@profile = profile。

#5


1  

this is more simple

这是更简单的

<%= render :partial => "profiles/show", :collection => @profiles %>

on partial _show.html.erb

在部分_show.html.erb

<%= profile.fullname %>

hope helped

希望帮助

#1


32  

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.

为什么在部分中使用实例变量(名称以“@”开头的变量,例如:@object)如此重要?这不是一个好习惯。在部分中使用实例变量会使控制流变得复杂,从而促进bug,并使部分的重用变得更加困难。这篇博文更深入地解释了这个问题。

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

你有两个选择。第一个选项是建议的解决方案。

  1. Change all instance variables to a local variable and pass it to the partial with the locals argument of render.

    将所有实例变量更改为局部变量,并将其传递给带局部变量的render。

  2. Set the instance variable before the partial is rendered. Partials have access to all the instance variables that your controller sets.

    在呈现局部变量之前设置实例变量。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.

部分中的实例变量是不好的。您不应该仅仅因为已经编写了部分来使用实例变量就设置实例变量。重写的部分。

#2


14  

Indeed, quoting this blog post you should know:

事实上,引用这篇博文你应该知道:

A partial is a reusable view 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 guides site explains some simple use cases:

rails指南站点解释了一些简单的用例:

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 show method, so probably would be a _show.html.erb file and a show.html.erb file.

额外提示:可能局部命名的show不是一个好的选择,给它一个与您要重用的东西相关的更有意义的名称。在一个RESTful场景中,当您有一个show方法时,这听起来更容易出错/混淆,所以可能是一个_show.html。erb文件和一个show.html。erb文件。

Hope this helped.

希望这个有帮助。

#3


4  

For Rails 3+, the answer to your actual question is:

对于Rails 3+,您实际问题的答案是:

<%= render 'profiles/show', :@profile => blah %>

Your partial will now see @profile locally.

您的部分现在将在本地看到@profile。

I'm not saying this is a good approach. I'm honestly not sure if this is ugly or not. But it works.

我不是说这是一个好方法。老实说,我不确定这是否丑陋。但它的工作原理。

#4


1  

You could always do @profile = profile in the partial.

可以在部分中使用@profile = profile。

#5


1  

this is more simple

这是更简单的

<%= render :partial => "profiles/show", :collection => @profiles %>

on partial _show.html.erb

在部分_show.html.erb

<%= profile.fullname %>

hope helped

希望帮助