1

嗨,我正在尝试从远程 rails 表单中提取一些参数。有问题的参数是 event_id。

在我的控制器中,我做了以下事情:

def create
  @availability = Availability.new(params[:availability])
  @exclude = Availability.where(:event_id => params[:event_id]).select(:user_id).pluck(:user_id)
  @players = User.where('team_id = ? and id NOT IN (?)', current_user[:team_id], @exclude).all
  @event = Event.where('id = ?', params[:event_id])

  respond_to do |format|
    if @availability.save
      format.html { redirect_to @availability, :notice => 'Availability was successfully created.' }
      format.js { @availabilities = Availability.all }
    else
      format.html { render :action => "new" }
      format.json { render :json => @availability.errors, :status => :unprocessable_entity }
    end
  end
end

触发此请求的操作是:

%li= button_to 'Add', availabilities_path(:availability => {:team_id => @event.team_id, :user_id => user, :event_id => @event.id }), :remote => true, :method => :post, :class => 'button tiny success', :disable_with => 'Add'

问题是为什么模型没有检测到 event_id?

 Started POST "/availabilities?availability%5Bevent_id%5D=75&availability%5Bteam_id%5D=1&availability%5Buser_id%5D=54" for 124.168.81.54 at 2013-02-12 08:48:42 +0100
Processing by AvailabilitiesController#create as JS
  Parameters: {"authenticity_token"=>"sebpDgAPPmK8q9024vrhN4eEQT7vZLr+pa5XdSicAMc=", "availability"=>{"event_id"=>"75", "team_id"=>"1", "user_id"=>"54"}}
  User Load (1.5ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 38 LIMIT 1
   (0.8ms)  SELECT user_id, user_id FROM "availabilities" WHERE "availabilities"."event_id" IS NULL
  User Load (1.3ms)  SELECT "users".* FROM "users" WHERE (team_id = 1 and id NOT IN (NULL))
   (0.4ms)  BEGIN
  Availability Exists (0.9ms)  SELECT 1 AS one FROM "availabilities" WHERE ("availabilities"."user_id" = 54 AND "availabilities"."event_id" = 75) LIMIT 1
  SQL (1.8ms)  INSERT INTO "availabilities" ("available", "comment", "created_at", "event_id", "team_id", "unique_id", "updated_at", "user_id") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id"  [["available", nil], ["comment", nil], ["created_at", Tue, 12 Feb 2013 18:48:42 EST +11:00], ["event_id", 75], ["team_id", 1], ["unique_id", nil], ["updated_at", Tue, 12 Feb 2013 18:48:42 EST +11:00], ["user_id", 54]]
   (19.4ms)  COMMIT
  Availability Load (0.9ms)  SELECT "availabilities".* FROM "availabilities" 
  Event Load (0.9ms)  SELECT "events".* FROM "events" WHERE (id = NULL)
  Rendered events/_progress_bar.html.haml (8.6ms)
  Rendered availabilities/create.js.erb (11.1ms)
Completed 500 Internal Server Error in 100ms
4

1 回答 1

4

因为event_id是在params[:availability][:event_id],不是params[:event_id]

于 2013-02-12T08:05:55.727 回答