受不了啦,给我一个理由阿: M 如下:
class Trip
include Mongoid::Document
include Mongoid::Timestamps
field :title, :type => String
has_many :steps, :dependent => :destroy
accepts_nested_attributes_for :steps
end
class Step
include Mongoid::Document
include Mongoid::Timestamps
field :content, :type => String
validates_presence_of :content
belongs_to :trip
end
C 如下:
class TripsController < ApplicationController
def new
@trip = Trip.new
respond_with(@trip) do |format|
format.html # new.html.erb
format.json { render json: trip }
end
end
......
V 如下:(haml)
= simple_form_for @trip do |f|
= f.input :title
= f.simple_fields_for @trip.steps.build do |sf|
= sf.input :content
= f.submit 'Save'
不知为何,form 页面看上去很正常:
<input class="string required" id="trip_step_content" name="trip[step][content]" required="required" size="50" type="text">
但 post 上去,params 就变成了:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"GgiUuDS8YcBmFhkt5jYGNXGBho/eVZuVQhB4K1S0KEo=", "trip"=>{"title"=>"111", "step"=>{"content"=>"111"}}, "commit"=>"Save"}
而 mongoid 要求的 step 参数却是:steps_attributes; 但凡能 stackoverflow 到的,都不会这样阿。 求教:mongoid 的 accepts_nested_attributes_for 应该如何写 viewer 阿; simple_field_for 如何写?