Rails 3:在视图中显示控制器中的变量

发布于 2024-11-25 21:41:58 字数 284 浏览 1 评论 0原文

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

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

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

有人可以指导一下吗?谢谢你!

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

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

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

Would anyone have any guidance? Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

卷耳 2024-12-02 21:41:58

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

因此,如果在控制器中定义了 @article_votes,则

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

在视图中的某个位置写入的内容应该会显示在页面中。

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

希望这有帮助。

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

So, if @article_votes is defined in the controller, writing

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

somewhere in your view should show up in your page.

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

Hope this helps.

与风相奔跑 2024-12-02 21:41:58

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

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

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

确保 @article_votes 是 nil 以外的类的实例。

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

例如

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

@article_vote = ArticleVote.first

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

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

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.

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.

Make sure that @article_votes is an instance of a class other than 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

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_vote = ArticleVote.first

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

北城半夏 2024-12-02 21:41:58

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

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

现在在您的部分中您可以使用

<%= article_votes %>

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

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

Now within your partial you can use

<%= article_votes %>
画离情绘悲伤 2024-12-02 21:41:58

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

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

失去的东西太少 2024-12-02 21:41:58

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

我有一个标准的“创建”块,如果未保存模型,它将返回错误。在本例中,我设置了一个实例变量,以便重新渲染将滚动到出现错误的表单。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文