I'm quite new to rails and I'm using grape to build an API rails app.
我对rails非常陌生,我正在使用grape来构建API rails应用程序。
I've been getting this error message after I click on the 'Create Event' button :-
点击“创建活动”按钮后,我收到此错误消息: -
NameError at /events
undefined local variable or method `event_params' for #<EventsController:0x00000008f76400>
The error points to the controller file for events,
该错误指向事件的控制器文件,
class EventsController < ApplicationController
before_action :authenticate_user!
before_action :authenticate_user_from_token!
before_action :set_event, only: [:show, :edit, :update, :destroy]
def index
@events = Event.all
if user_signed_in?
if current_user.is_admin?
@events = Event.all
respond_to do |format|
format.html
format.json { render json: @events }
end
else
@events = current_user.events
respond_to do |format|
format.html
format.json { render json: @events }
end
end
else
render json: {}, status: :unauthorized
end
end
def new
@event = Event.new
end
def edit
end
def create
@event = current_user.events.new(event_params)
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 :show, notice: 'You are not allowed to view this.' }
format.json { render json: {}, status: :unauthorized}
end
end
end
And here's the event_params method,
这是event_params方法,
def event_params
params.require(:event).permit(:event_name, :event_description, :event_date, :event_time)
end
The log file shows,
日志文件显示,
Started POST "/events" for 127.0.0.1 at 2015-08-10 14:38:16 +0800
Processing by EventsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"yRVn4uuxpMVejs/gsG004PIi2JRaWnovGBm7TweGgkf2XpTLzjOZNGanCRVVKSPAV6JzNRWmDzRRbq/dC2KOKQ==", "event"=>{"event_name"=>"ddwq", "event_description"=>"rqreqreqr", "event_date(1i)"=>"2015", "event_date(2i)"=>"8", "event_date(3i)"=>"10", "event_time(1i)"=>"2015", "event_time(2i)"=>"8", "event_time(3i)"=>"10", "event_time(4i)"=>"06", "event_time(5i)"=>"38"}, "commit"=>"Create Event"}
[1m[36mUser Load (1.6ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1[0m [["id", 1]]
Completed 500 Internal Server Error in 34ms
NameError - undefined local variable or method `event_params' for #<EventsController:0x000000082a6540>:
app/controllers/events_controller.rb:37:in `create'
actionpack (4.2.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
...
...
bin/rails:4:in `<main>'
Started POST "/__better_errors/beb9cf3b65f0e36a/variables" for 127.0.0.1 at 2015-08-10 14:38:16 +0800
4 个解决方案
#1
0
def create
@event = current_user.events.new(event_params)
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
respond_to do |format|
format.html { render :show, notice: 'You are not allowed to view this.' }
format.json { render json: {}, status: :unauthorized}
end
end ##You have missed the end here
And here's the event_params method,
这是event_params方法,
def event_params
params.require(:event).permit(:event_name, :event_description, :event_date, :event_time)
end
#2
0
Try build instead of new replace
尝试构建而不是新的替换
@event = current_user.events.new(event_params)
@event = current_user.events.new(event_params)
by
@event = current_u
ser.events.build(event_params)`
@event = current_user.events.build(event_params)`
#3
0
The error message indicates that, event_params
method is not available for the EventsController
. I think you mistakenly put the event_params
inside another method or missed an end
to end the create
method. It should be an instance method of your EventsController
class.
错误消息指示event_params方法不可用于EventsController。我认为你错误地将event_params放在另一个方法中或错过了结束create方法的结束。它应该是EventsController类的实例方法。
This code should work as it is:
此代码应该按原样运行:
class EventsController < ApplicationController
before_action :authenticate_user!
before_action :authenticate_user_from_token!
before_action :set_event, only: [:show, :edit, :update, :destroy]
def index
@events = Event.all
if user_signed_in?
if current_user.is_admin?
@events = Event.all
respond_to do |format|
format.html
format.json { render json: @events }
end
else
@events = current_user.events
respond_to do |format|
format.html
format.json { render json: @events }
end
end
else
render json: {}, status: :unauthorized
end
end
def new
@event = Event.new
end
def edit
end
def create
@event = current_user.events.new(event_params)
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 :show, notice: 'You are not allowed to view this.' }
format.json { render json: {}, status: :unauthorized}
end
end
end
private
def event_params
params.require(:event).permit(:event_name, :event_description, :event_date, :event_time)
end
end
#4
0
So, the error was a missing 'end' for show method which I didn't post that part.
因此,错误是show方法缺少'结束',我没有发布该部分。
I completely missed that part because the error was misleading and I assume that it has something to do with the event parameters since the error points there.
我完全错过了那个部分,因为错误是误导性的,我认为它与事件参数有关,因为错误指向那里。
#1
0
def create
@event = current_user.events.new(event_params)
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
respond_to do |format|
format.html { render :show, notice: 'You are not allowed to view this.' }
format.json { render json: {}, status: :unauthorized}
end
end ##You have missed the end here
And here's the event_params method,
这是event_params方法,
def event_params
params.require(:event).permit(:event_name, :event_description, :event_date, :event_time)
end
#2
0
Try build instead of new replace
尝试构建而不是新的替换
@event = current_user.events.new(event_params)
@event = current_user.events.new(event_params)
by
@event = current_u
ser.events.build(event_params)`
@event = current_user.events.build(event_params)`
#3
0
The error message indicates that, event_params
method is not available for the EventsController
. I think you mistakenly put the event_params
inside another method or missed an end
to end the create
method. It should be an instance method of your EventsController
class.
错误消息指示event_params方法不可用于EventsController。我认为你错误地将event_params放在另一个方法中或错过了结束create方法的结束。它应该是EventsController类的实例方法。
This code should work as it is:
此代码应该按原样运行:
class EventsController < ApplicationController
before_action :authenticate_user!
before_action :authenticate_user_from_token!
before_action :set_event, only: [:show, :edit, :update, :destroy]
def index
@events = Event.all
if user_signed_in?
if current_user.is_admin?
@events = Event.all
respond_to do |format|
format.html
format.json { render json: @events }
end
else
@events = current_user.events
respond_to do |format|
format.html
format.json { render json: @events }
end
end
else
render json: {}, status: :unauthorized
end
end
def new
@event = Event.new
end
def edit
end
def create
@event = current_user.events.new(event_params)
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 :show, notice: 'You are not allowed to view this.' }
format.json { render json: {}, status: :unauthorized}
end
end
end
private
def event_params
params.require(:event).permit(:event_name, :event_description, :event_date, :event_time)
end
end
#4
0
So, the error was a missing 'end' for show method which I didn't post that part.
因此,错误是show方法缺少'结束',我没有发布该部分。
I completely missed that part because the error was misleading and I assume that it has something to do with the event parameters since the error points there.
我完全错过了那个部分,因为错误是误导性的,我认为它与事件参数有关,因为错误指向那里。