开发者

Ruby on Rails 3 .each do Problem

开发者 https://www.devze.com 2023-03-25 18:34 出处:网络
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.

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.

Running 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开发者_如何学C.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

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

should be

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


.each returns the entire array at the end once it is finished the loop. <%= ... %> prints out the value of the statment, which is the value returned by .each <% ... %> does not. So you want:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号