我收到一个我似乎不理解的错误。任何帮助将非常感激。
控制台:
Started GET "/users" for 127.0.0.1 at 2014-12-24 15:54:01 +0000
Processing by UsersController#index as HTML
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = 40 ORDER BY "users"."id" ASC LIMIT 1
Rendered users/index.html.erb within layouts/application (1.1ms)
Completed 500 Internal Server Error in 5ms
ActionView::Template::Error (undefined method `group_by' for nil:NilClass):
10: </tr>
11:
12:
13: <% @events.group_by(&:user).each do |user, events| %>
14: <%= link_to(image_tag("profile_image_gravatar.png"), user) %>
15: <%= user.full_name %>
16: <%= link_to 'Show', user %>
app/views/users/index.html.erb:13:in `_app_views_users_index_html_erb__3564202377022035077_70335954377780'
Rendered /Users/ARTLoe/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.3ms)
Rendered /Users/ARTLoe/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.0ms)
Rendered /Users/ARTLoe/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout (9.5ms)
events_controller.rb
class EventsController < ApplicationController
before_action :set_event, only: [:show, :edit, :update, :destroy]
before_filter :authenticate_user!
def index
@events = Event.order(:date)
# @events = current_user.events | displays only events by current user
end
def show
@commentable = @event
@comments = @commentable.comments
@comment = Comment.new
end
def new
@event = Event.new
end
def edit
end
def create
@event = Event.new(event_params)
@event.user = current_user
respond_to do |format|
if @event.save
format.html { redirect_to @event, notice: 'Event was successfully created.' }
format.json { render :show, status: :created, location: @event }
else
format.html { render :new }
format.json { render json: @event.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @event.update(event_params)
format.html { redirect_to @event, notice: 'Event was successfully updated.' }
format.json { render :show, status: :ok, location: @event }
else
format.html { render :edit }
format.json { render json: @event.errors, status: :unprocessable_entity }
end
end
end
def destroy
@event.destroy
respond_to do |format|
format.html { redirect_to events_url, notice: 'Event was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_event
@event = Event.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def event_params
params.require(:event).permit(:name, :description, :date, :time, :city, :price, :user_id)
end
end
users_controller.rb
class UsersController < ApplicationController
before_action :set_user, only: [:show]
before_filter :authenticate_user!
def index
@users = User.all
end
def show
@user = User.find(params[:id])
@user_events = @user.events.order(:date)
@events = Event.all
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:email, :password, :password_confirmation, :firstname, :lastname, :dob, :gender, :description, :role)
end
end
用户/ index.html.erb
<% provide(:title, 'Users') %>
<h1>Listed Event Organizers</h1>
<% @events.group_by(&:user).each do |user, events| %>
<%= link_to(image_tag("profile_image_gravatar.png"), user) %>
<%= user.full_name %>
<%= link_to 'Show', user %>
<% end %>
<h6><%= link_to 'Create Event', new_event_path %></h6>
答案 0 :(得分:2)
您未在@events
中定义UsersController#index
。
答案 1 :(得分:1)
您的操作未定义您尝试在视图中使用的实例变量。
在index
中的UsersController
操作中,您需要定义@events
变量。
您EventsController
class class UsersController < ApplicationController
def index
@users = User.all
@events = Event.includes(:user)
end
end