Reputation: 502
I am receiving a undefined method `name' for # error for my Rails project. I know that name is defined for events by double checking using my console, so I don't understand why it won't load. Here is some of my code:
Apps Show:
<h1><%= @app.title %></h1>
<h5>App URL: <%= @app.url %></h5>
<h5>App User_id: <%= @app.user_id %></h5>
<br>
<h2> Events </h2>
<% @events.each do |event| %>
<div>
<%= event.name %><span class="badge"><%= event.count %></span>
</div>
<% end %>
<br>
<%= link_to "Edit", edit_app_path(@app), class: 'btn btn-success' %>
Apps Controller:
class AppsController < ApplicationController
def create
@app = App.new(params.require(:app).permit(:title, :url))
@app.user_id = 1
if @app.save
flash[:notice] = "App was saved!"
redirect_to @app
else
flash[:error] = "There an error, oh noes!Please try again!"
render :new
end
end
def new
@app = App.new
end
def show
@app = App.find(params[:id])
@events = @app.events.group_by(&:name)
end
def index
@apps = App.all
end
def edit
@app = App.find(params[:id])
end
def update
@app = App.find(params[:id])
if @app.update_attributes(params.require(:app).permit(:title, :url))
flash[:notice] = "Application was updated."
redirect_to @app
else
flash[:error] = "There was an error saving the app. Please try again."
render :edit
end
end
def destroy
@app = App.find(params[:id])
if @app.destroy
flash[:notice] = "App was removed."
redirect_to @app
else
flash[:error] = "App couldn't be deleted. Try again."
redirect_to @app
end
respond_to do |format|
format.html
format.js
end
end
end
Events Seed:
create_table "events", force: :cascade do |t|
t.integer "app_id"
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
App Model:
class App < ActiveRecord::Base
belongs_to :user
has_many :events
end
Complete Error Log:
Completed 500 Internal Server Error in 19ms
ActionView::Template::Error (undefined method `name' for #<Array:0x007fcc53394af0>):
6: <h2> Events </h2>
7: <% @events.each do |event| %>
8: <div>
9: <%= event.name %><span class="badge"><%= event.count %></span>
10: </div>
11: <% end %>
12: <br>
app/views/apps/show.html.erb:9:in `block in _app_views_apps_show_html_erb__2949113196422864311_70257764571040'
app/views/apps/show.html.erb:7:in `each'
app/views/apps/show.html.erb:7:in `_app_views_apps_show_html_erb__2949113196422864311_70257764571040'
Rendered /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_source.erb (7.1ms)
Rendered /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (4.1ms)
Rendered /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.9ms)
Rendered /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout (71.2ms)
Rendered /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/web-console-2.1.3/lib/web_console/templates/_markup.html.erb (0.7ms)
Rendered /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/web-console-2.1.3/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.7ms)
Rendered /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/web-console-2.1.3/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.5ms)
Rendered /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/web-console-2.1.3/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.3ms)
Rendered /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/web-console-2.1.3/lib/web_console/templates/console.js.erb within layouts/javascript (69.6ms)
Rendered /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/web-console-2.1.3/lib/web_console/templates/main.js.erb within layouts/javascript (0.4ms)
Rendered /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/web-console-2.1.3/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.7ms)
Rendered /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/web-console-2.1.3/lib/web_console/templates/index.html.erb (156.5ms)
Upvotes: 0
Views: 62
Reputation: 230561
In cases like this, simple debug printing will reveal the problem. Is name
not defined on @events
when you think it should be? Are you sure that @events
is what you think it is?
Add p @events
line to your controller, retry the request and read the log. A piece of advice for the future. :)
The problem starts here:
@events = @app.events.group_by(&:name)
I'm pretty sure that output of group_by
is a hash, where keys are different names and values are arrays of events which all have the same name. (depends on what exactly the implementation of group_by
is, of course)
<% @events.each do |event| %>
event
here is not an instance of Event
class, but a two element array, [name, [event1, event2, ...]]
. You should iterate this nested array to get real events, which do have the method you want.
<% @events.each do |name, events| %>
<%= name %> <%= events.length %>
Upvotes: 1
Reputation: 101
@events is hash you have to iterate like
<% @events.each do |event| %>
<div>
<%= event[0] %><span class="badge"><%= event[1].count %></span>
</div>
<% end %>
Upvotes: 1