Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在application\u控制器中使用before\u过滤器访问application.html.erb中的模型会导致意外的html_Html_Ruby On Rails_Ruby_Ruby On Rails 4 - Fatal编程技术网

在application\u控制器中使用before\u过滤器访问application.html.erb中的模型会导致意外的html

在application\u控制器中使用before\u过滤器访问application.html.erb中的模型会导致意外的html,html,ruby-on-rails,ruby,ruby-on-rails-4,Html,Ruby On Rails,Ruby,Ruby On Rails 4,我想访问application.html.erb文件中的模型。为此,我在application_controller.rb中定义了一个before_过滤器: before_filter :populate_ages protected def populate_ages @ages= Ages.all end 在my application.html.erb中,我有以下内容: <%= @ages.each do |age| %> <p><%= age.

我想访问application.html.erb文件中的模型。为此,我在application_controller.rb中定义了一个before_过滤器:

before_filter :populate_ages

protected

def populate_ages
  @ages= Ages.all
end
在my application.html.erb中,我有以下内容:

<%= @ages.each do |age| %>
  <p><%= age.name %></p>
<% end %>

名称被正确呈现,但我得到了额外的输出,即数组@ages。看起来是这样的:

[#&lt;ageid: 1, name: &quot;Herren&quot;, created_at: &quot;2014-06-26 08:02:58&quot;, updated_at: &quot;2014-06-26 08:02:58&quot;&gt;, #&lt;ageid: 2, name: &quot;A-Jugend&quot;, created_at: &quot;2014-06-26 08:02:58&quot;, updated_at: &quot;2014-06-26 08:02:58&quot;&gt;, #&lt;ageid: 3, name: &quot;B-Jugend&quot;, created_at: &quot;2014-06-26 08:02:58&quot;, updated_at: &quot;2014-06-26 08:02:58&quot;&gt;]           </ul>
[#ageid:1,名称:“Herren”,创建时间:“2014-06-26 08:02:58”,更新时间:“2014-06-26 08:02:58”,更新时间:“A-Jugend:2,名称:“A-Jugend”,创建时间:“2014-06-26 08:02:58”,更新时间:“2014-06-26 08:02:58”,更新时间:“B-Jugend:3,名称:“B-Jugend”,创建时间:“2014-06-26 08:02:58”,更新时间:“2014-06-26:08:58”

我在导航栏中有这个代码。因此,我的导航栏被这个附加文本扩展了。在主体中添加循环将导致相同的输出

如果不希望循环上出现=符号,请将其更改为:

<% @ages.each do |age| %>
  <p><%= age.name %></p>
<% end %>

通过让
只需写为

<% @ages.each do |age| %>
  <p><%= age.name %></p>
<% end %>

@ages
是一个
数组
实例。现在,当完整迭代完成时,返回接收器本身
表示您正在告诉执行每个m,方法并打印
#每个
方法的结果,这就是您所面临的问题。因此,告诉你的模板执行了这个方法,不要打印它的结果,正确的语法是