我对rails非常陌生,我使用grape来构建API rails应用程序。
点击“创建活动”后,我收到了此错误消息。按钮: -
NameError at /events
undefined local variable or method `event_params' for #<EventsController:0x00000008f76400>
错误指向事件的控制器文件,
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
这里是event_params方法,
def event_params
params.require(:event).permit(:event_name, :event_description, :event_date, :event_time)
end
日志文件显示,
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
答案 0 :(得分:0)
尝试构建而不是新建 取代
@event = current_user.events.new(event_params)
通过
@event = current_u
ser.events.build(event_params)`
答案 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
这里是event_params方法,
def event_params
params.require(:event).permit(:event_name, :event_description, :event_date, :event_time)
end
答案 2 :(得分:0)
错误消息表明event_params
方法无法使用EventsController
方法。我认为您错误地将event_params
置于另一个方法中或错过了end
来结束create
方法。它应该是EventsController
类的实例方法。
此代码应该按原样运行:
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
答案 3 :(得分:0)
所以,错误是一个缺失的结束&#39;对于show not method我没有发布那个部分。
我完全错过了那个部分,因为错误是误导性的,我认为它与事件参数有关,因为错误指向那里。