简体   繁体   中英

Strong parameters and nested attributes

I am upgrading from Rails 3 to 4, and so I am adopting strong parameters. It seems that nested attributes are not being passed successfully. I have read several related SO questions and blog posts, and I'm still stumped.

An Event has many Occurrences . When I submit the form to create a new Event and one or more Occurrences , I get "1 error prohibited this class from being saved: Occurrences start can't be blank." However, start is not blank, as I confirmed by looking at the Parameters that are posted:

{"utf8"=>"✓",
 "authenticity_token"=>"aPRLtUbjW7EMxO2kWSzCEctHYZgvBvwuk2QUymfiwkM=",
 "event"=>{"name"=>"some name",
 "photo"=>#<ActionDispatch::Http::UploadedFile:0x007f9e9bc95310 @tempfile=#    <File:/var/folders/rz/1p7tmbmj2t5fbfv2wjhvwcsh0000gn/T/RackMultipart20140927-52743-10pcxtg>,
 @original_filename="10435871_671176211140_3488560836686101357_n.jpg",
 @content_type="image/jpeg",
 @headers="Content-Disposition: form-data; name=\"event[photo]\"; filename=\"10435871_671176211140_3488560836686101357_n.jpg\"\r\nContent-Type: image/jpeg\r\n">,
 "summary"=>"",
 "facebook_event_link"=>"",
 "occurrences_attributes"=>{"0"=>{"start"=>"09/30/2014",
 "_destroy"=>"0"}},
 "special"=>"0",
 "prose"=>""},
 "commit"=>"Create Event"}

Here are the relevant sections of the models and controller.

app/models/event.rb:

class Event < ActiveRecord::Base

  has_many :occurrences, :dependent => :destroy
  accepts_nested_attributes_for :occurrences, allow_destroy: true, reject_if: proc {|o| o[:start].blank? }

app/models/occurrence.rb:

class Occurrence < ActiveRecord::Base
  belongs_to :event

  validates_presence_of :start

app/controllers/events_controller.rb:

class EventsController < ApplicationController

  def new
    @event = Event.new
    @event.occurrences.build
    @event.passes.build
  end

  def create
    @event = Event.create(event_params)

    if @event.save
      redirect_to @event, notice: 'Event was successfully created.'
    else
      render :action => "new"
    end
  end

  ...

  private

    def event_params
      params.require(:event).permit(
        :name, :expiration, :prose, :special, :summary, :photo, :link, :price, :student_price, :registration_switch, :facebook_event_link,
        :occurrences_attributes => [:id, :start, :end, :_destroy])
    end
end

Why isn't the information about Occurrences being passed correctly?

I'm not sure, but I think that strong parameters requires you permit the foreign key on associated models. So, perhaps you are missing event_id in your occurrences_attributes?

Not even close to 100% sure, but this could be it:

def event_params
  params.require(:event).permit(
    :name, :expiration, :prose, :special, :summary, :photo, :link, :price, :student_price, :registration_switch, :facebook_event_link,
    :occurrences_attributes => [:id, :start, :end, :_destroy, :event_id])
end

也许你需要确保你的表单有multipart: true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM