我认为这是一个基本的创建操作,并且记录已保存,但是当我尝试重定向时,我不断收到错误“未定义的方法`状态'为nil:NilClass”。
“状态”是记录中的一列......但是记录正在保存到数据库中。我不知道为什么它不坚持。如果我将重定向路由更改为没有@event参数的“root_path”,它仍然会查找@event类。即使我让post方法说JUST重定向,它也会失败。我已经尝试删除每个操作之前它仍然寻找并且没有找到一些记录(我甚至没有告诉它寻找!)。我查看了模型,看看是否有任何回调都是行为不端。奇怪的是'保存'成功',但任何重定向失败。
EventsController.rb
def create
@event = Event.new(event_params)
respond_to do |format|
if @event.save
format.html { redirect_to project_project_date_path(project_id: @event.project_date.project_id, id: @event.project_date_id, event_id: @event.id), notice: 'Event was successfully created.' }
format.json { render :show, status: :created, location: @event.project_date }
else
format.html { redirect_to project_project_date_path(project_id: @event.project_date.project_id, id: @event.project_date_id, event_id: @event.id), alert: 'something went wrong' }
format.json { render :show, status: :created, location: @event.project_date }
end
end
end
routes.rb
resources :events, only: [:create]
resources :projects do
resources :project_dates, only: [:show]
end
_form.html.slim
= simple_form_for @event do |f|
堆栈跟踪......
08:25:24 web.1 |
08:25:24 web.1 | Started POST "/events" for 127.0.0.1 at 2016-01-29 08:25:24 -0800
08:25:24 web.1 | Processing by EventsController#create as HTML
08:25:24 web.1 | Parameters: {"utf8"=>"✓", "authenticity_token"=>"3LpsRTpupzPKRWm+cNq/pxQZMhGvlXS9d35GdVRR0dK+i2ZGsRKOOeIOXzMWUEQLpLGrML2gYemeKMP2/vKwaQ==", "event"=>{"project_id"=>"1", "project_date_id"=>"11", "participant_id"=>"7", "editor_participant_id"=>"7", "category"=>"Meals", "address_id"=>"2", "status"=>"pending", "description"=>"sfsdf"}, "commit"=>"ADD "}
08:25:24 web.1 | User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 7]]
08:25:24 web.1 | Profile Load (0.8ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."user_id" IN (7)
08:25:24 web.1 | (0.2ms) BEGIN
08:25:24 web.1 | ProjectDate Load (0.4ms) SELECT "project_dates".* FROM "project_dates" WHERE "project_dates"."id" = $1 ORDER BY "project_dates"."schedule_date" ASC LIMIT 1 [["id", 11]]
08:25:24 web.1 | SQL (1.3ms) INSERT INTO "events" ("project_date_id", "project_id", "participant_id", "description", "status", "editor_participant_id", "category", "address_id", "date_start", "date_end", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING "id" [["project_date_id", 11], ["project_id", 1], ["participant_id", 7], ["description", "sfsdf"], ["status", 3], ["editor_participant_id", 7], ["category", "Meals"], ["address_id", 2], ["date_start", "2016-02-02"], ["date_end", "2016-02-02"], ["created_at", "2016-01-29 16:25:24.764942"], ["updated_at", "2016-01-29 16:25:24.764942"]]
08:25:24 web.1 | SQL (0.6ms) UPDATE "project_dates" SET "events_count" = COALESCE("events_count", 0) + 1 WHERE "project_dates"."id" = $1 [["id", 11]]
08:25:24 web.1 | (2.0ms) COMMIT
08:25:24 web.1 | Redirected to
08:25:24 web.1 | Completed 500 Internal Server Error in 39ms (ActiveRecord: 5.6ms)
08:25:24 web.1 |
08:25:24 web.1 | NoMethodError - undefined method `status' for nil:NilClass:
08:25:24 web.1 | actionpack (4.2.4) lib/action_controller/metal/instrumentation.rb:65:in `block in redirect_to'
08:25:24 web.1 | activesupport (4.2.4) lib/active_support/notifications.rb:164:in `block in instrument'
08:25:24 web.1 | activesupport (4.2.4) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
08:25:24 web.1 | activesupport (4.2.4) lib/active_support/notifications.rb:164:in `instrument'
ProjectDatesController.rb
def show
@project = Project.find(params[:project_id])
@project_date = ProjectDate.find(params[:id])
@current_user_participation = Participant.find(7)
@event = Event.new(project_id: @project.id, project_date_id: @project_date.id )
@project_dates = @project.project_dates
current = @project_dates.index(@project_date)
@next = @project_dates[current + 1] if @project_dates.length >= current + 1
@prev = @project_dates[current - 1] if current != 0
@first = @project_dates.first if current != 0
@last = @project_dates.last if current + 1 < @project_dates.length
@current = current + 1
@events = @project_date.events
if params[:event_id].present?
@chosen_event = Event.find(params[:event_id])
end
end
def project_date_params
params.require(:project_date).permit(:id,
events_attributes: [:id, :project_date_id, :project_id, :participant_id, :description, :status, :editor_participant_id,
:date_start, :date_end, :all_day, :repeat, :time_start, :time_end, :category, :attendees_count] )
end