Ruby-on-rails Rails 3:在视图中显示控制器中的变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6812341/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:39:19  来源:igfitidea点击:

Rails 3: Display variable from controller in view

ruby-on-railsvariablesviewcontroller

提问by Ben

I'm pretty new to rails and I was wondering how I can display a variable from my controller into my view.

我对 rails 很陌生,我想知道如何将控制器中的变量显示到我的视图中。

Basically, I have 2 files: articles_controller.rband _article.html.erb

基本上,我有 2 个文件:articles_controller.rb_article.html.erb

In my controller file I have a variable @article_votesthat I want to display on my view. I've tried using <%= @article_votes %>but it does not display anything at all.

在我的控制器文件中,我有一个@article_votes要在视图中显示的变量。我试过使用,<%= @article_votes %>但它根本不显示任何内容。

Would anyone have any guidance? Thank you!

有人会有任何指导吗?谢谢!

回答by nathanvda

Not sure how new you are to rails. Any instance variable you define inside your controller is reachable inside the views.

不知道你对 Rails 有多陌生。您在控制器中定义的任何实例变量都可以在视图中访问。

So, if @article_votesis defined in the controller, writing

所以,如果@article_votes在控制器中定义,写

<pre>
  <%= @article_votes.inspect %>
</pre>

somewhere in your view should show up in your page.

您视图中的某个位置应显示在您的页面中。

But the _article.html.erbis not shown by default. If you have a showmethod in your ArticlesController, the view called show.html.erbis rendered. This is a general approach, so for the indexmethod, it should be called index.html.erb.

_article.html.erb默认情况下不显示。如果您的 中有一个show方法,则会呈现ArticlesController调用的视图show.html.erb。这是一种通用方法,因此对于index方法,应调用index.html.erb.

Hope this helps.

希望这可以帮助。

回答by jamesc

The output of <%= @article_votes %> will depend on what class the @article_votes is an object of and whether or not you are actually rendering that _article.html.erb template

<%= @article_votes %> 的输出将取决于 @article_votes 是哪个类的对象以及您是否实际渲染该 _article.html.erb 模板

Just call <%= render :partial => 'article' %> in the index.html.erb file or whatever .html.erb file is named after the controller action that you are using to try to render this view.

只需在 index.html.erb 文件中调用 <%= render :partial => 'article' %> 或以您用来尝试呈现此视图的控制器操作命名的任何 .html.erb 文件。

Normally coming from a controller @article_votes would be an array (by convention) plural names denote an array of objects and a singular name denotes a specific object of an ActiveRecord class based on a table called article_votes which would contain all the values held in the table for a specific record.

通常来自控制器@article_votes 将是一个数组(按照惯例)复数名称表示一个对象数组,单数名称表示基于名为 article_votes 的表的 ActiveRecord 类的特定对象,该表将包含表中保存的所有值为特定记录。

Make sure that @article_votes is an instance of a class other than nil

确保@article_votes 是一个非 nil 类的实例

If it's just a string then (@article_votes = "A string") then you will see that in your view. If it's a result of a call ActiveRecord to find all objects from the ArticleVotes model then it will be an array which will be empty if nothing is in the table. You will need to get a specific object from that array and then you will need to choose which of the available methods on that object you wish to display

如果它只是一个字符串,那么 (@article_votes = "A string") 那么你会在你的视图中看到它。如果它是调用 ActiveRecord 从 ArticleVotes 模型中查找所有对象的结果,那么它将是一个数组,如果表中没有任何内容,该数组将为空。您需要从该数组中获取特定对象,然后您需要选择要显示该对象上的哪些可用方法

e.g.

例如

Assuming the table article_votes has a column called name on it and you have 1 article_vote record in that table with the value of 'This is a name' then the following code in the a controller action would get you a display of "This is a name" on your screen

假设表 article_votes 上有一个名为 name 的列,并且您在该表中有 1 个 article_vote 记录,其值为“这是一个名称”,那么控制器操作中的以下代码将使您显示“这是一个名称” “在你的屏幕上

@article_vote = ArticleVote.first

@article_vote = ArticleVote.first

If you had <%= @article_vote.name %> in your partial and assuming that you actually are rendering that partial

如果您的部分中有 <%= @article_vote.name %> 并假设您实际上正在渲染该部分

回答by Trent

If you are calling a partial you will need to pass the @article_votes variable to it.

如果您正在调用部分,则需要将 @article_votes 变量传递给它。

<@= render 'article', :article_votes => @article_votes %>

Now within your partial you can use

现在在你的部分你可以使用

<%= article_votes %>

回答by Arti

_article.html.erbis a partial view. Are you sure that in browser you're displaying proper action?

_article.html.erb是局部视图。您确定在浏览器中显示正确的操作吗?

回答by gwnp

Another important thing I just ran into, is that your variables values must be set before your call to render.

我刚刚遇到的另一件重要事情是,必须在调用渲染之前设置变量值。

I had a standard "create" block where if the model wasn't saved, it would return errors. In this case, I was setting an instance variable so the re-render would scroll to the form where the errors were.

我有一个标准的“创建”块,如果模型没有保存,它会返回错误。在这种情况下,我设置了一个实例变量,以便重新渲染会滚动到错误所在的表单。

I couldn't figure out why my variable wasn't being set when I clearly was setting it. As soon as I moved it above the "render action: :new" it worked.

当我清楚地设置它时,我无法弄清楚为什么我的变量没有被设置。一旦我将它移到“渲染操作::新”上方,它就起作用了。