未允许的参数:白名单参数后出错

时间:2014-08-22 13:12:40

标签: ruby-on-rails ruby-on-rails-4

我收到了一个“未经许可的参数:: event_id,:attendee_id”,即使我将这些参数列入白名单

Started POST "/planned_events" for 127.0.0.1 at 2014-08-22 22:08:39 +0900
Processing by PlannedEventsController#create as HTML
  Parameters: {"utf8"=>"✓",     
"authenticity_token"=>"WhVcJWjo08KWqxFViUPbSZVMYzu2UgGW9E+WRExP9Wc=", "planned_event"=>      
{":event_id"=>"3", ":attendee_id"=>"1"}, "commit"=>"Attend"}
Unpermitted parameters: :event_id, :attendee_id
   (0.0ms)  begin transaction
SQL (0.4ms)  INSERT INTO "planned_events" ("created_at", "updated_at") VALUES (?, ?)      
[["created_at", Fri, 22 Aug 2014 13:08:39 UTC +00:00], ["updated_at", Fri, 22 Aug 2014  
13:08:39 UTC +00:00]]
(1.8ms)  commit transaction
Unpermitted parameters: :event_id, :attendee_id
Completed 500 Internal Server Error in 9ms

ActionController::UrlGenerationError (No route matches {:action=>"show",     
:controller=>"events", :id=>nil} missing required keys: [:id]):
app/controllers/planned_events_controller.rb:6:in `create'

这是controllers / planned_events_controller.rb

class PlannedEventsController < ApplicationController

def create
    @planned_event = PlannedEvent.new(planned_event_params)
    if @planned_event.save
        redirect_to event_path(planned_event_params[:event_id])
    end
end

def destroy
    @planned_event = PlannedEvent.find(params[:id]).destroy
    redirect_to event_path(planned_event_params[:event_id] => :id)
end

private

    def planned_event_params
        params.require(:planned_event).permit(:event_id, :attendee_id)
    end
end

为什么?

broswer突出显示错误的“redirect_to ...”行。

ActionController::UrlGenerationError in PlannedEventsController#create
No route matches {:action=>"show", :controller=>"events", :id=>nil} missing required keys:    
[:id]

我假设:id为nil,因为它不接受我传递的id(来自白名单的params),并且找不到替代方案。

2 个答案:

答案 0 :(得分:0)

您的参数名称错误:

{":event_id"=>"3", ":attendee_id"=>"1"}

在表单中更改它们,传入的参数应为

{"event_id"=>"3", "attendee_id"=>"1"}

此外,您可以将整个redirect_to更改为更短的版本:

redirect_to @planned_event.event

答案 1 :(得分:0)

是路线错误。而不是转移到create移动到控制器的show行为。

<强>记住: -

your create link should be post
your show action should be get(with id passed)

我认为您需要查看自己的观点。点击link_tosubmit buttonform_for