Rails 3:显示控制器的变量 [英] Rails 3: Display variable from controller in view

查看:181
本文介绍了Rails 3:显示控制器的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对rails很新,我想知道如何显示一个变量从我的控制器到我的视图。



基本上,我有两个文件: articles_controller.rb _article.html.erb



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



任何人都有任何指导?谢谢!

解决方案

不知道你是多么新。



所以,如果 @article_votes 定义在你的控制器中定义的任何实例变量控制器,写

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

视图中的某处应显示在您的页面中。



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

希望这有助于。


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!

解决方案

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.

这篇关于Rails 3:显示控制器的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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