简体   繁体   English

Ruby on Rails 3。每个都有问题

[英]Ruby on Rails 3 .each do Problem

I'm sort of new to Ruby on Rails and have been learning just fine, but have seem to run into a problem that I can't seem to solve. 我是Ruby on Rails的新手,并且学习得很好,但似乎遇到了一个我似乎无法解决的问题。

Running Rails 3.0.9 & Ruby 1.9.2 运行Rails 3.0.9和Ruby 1.9.2

I'm running the following statement in my View: 我在我的视图中运行以下语句:

<%= @events.each do |f| %>
    <%= f.name %><%= link_to "View", event_path(f) %><br/><hr/>
<% end %>

And this in my controller: 这在我的控制器中:

class AdminController < ApplicationController
  def flyers
    @events = Event.all
  end
end

This goes through each of the records and outputs the appropriate name, but the problem is that at the end it displays all of the information for all of the records like so: 这会遍历每个记录并输出相应的名称,但问题是最后它会显示所有记录的所有信息,如下所示:

    [#<User id: 1, username: "test account", email: "test@gmail.com", password_hash:    "$2a$10$Rxwgy.0ZEOb0lMGEIliPBeB/jPSp8roeKdbMvXcLi32R...", password_salt: "$2a$10$Rxwgy.0ZEOb0lMGEIliPBe", created_at: 2111359287.2303703, updated_at: 2111359287.2303703, isadmin: true>]

I'm new to this site, so I'm not sure if you need any more details, but any help is appreciated, after all, I'm still learning. 我是这个网站的新手,所以我不确定你是否需要更多的细节,但毕竟我还在学习任何帮助。

Thanks in advance. 提前致谢。

You should be using <% , not <%= for your .each line, so 你应该使用<% ,而不是<%=作为你的.each行,所以

<%= @events.each do |f| %>

should be 应该

<% @events.each do |f| %>

.each returns the entire array at the end once it is finished the loop. .each完成循环后返回整个数组。 <%= ... %> prints out the value of the statment, which is the value returned by .each <% ... %> does not. <%= ... %>打印出该参数的值,该值是.each <% ... %>返回的值。 So you want: 所以你要:

<% @events.each do |f| %>
    <%= f.name %><%= link_to "View", event_path(f) %><br/><hr/>
<% end %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM