I have a mode Car
that has many services.
class Car < ActiveRecord::Base
has_many :car_services, dependent: :destroy
has_many :services, through: :car_services
end
In the new
controller action:
@car = Car.new
@car.car_services.build
And in the view _form.html.erb
(the relevant part):
<div class="edit_shipper_services">
<%= f.association :services,
as: :check_boxes,
label_method: :label,
value_method: :id,
label: 'Services' %>
</div>
The checkboxes are nicely rendered, but when I try to create a new car, I get an error about
undefined method `name' for nil:NilClass
I have some imported data in the database and when I try to save an existing information, the same error will occur.
How I am creating a new car:
respond_to do |format|
if @car.save
...
What am I missing?
Thank you.
it looks like your trying to save car and car_services in one form, in order too save the two in one form you need to use nested forms.
if you don't know how to use nested forms check out http://www.amooma.de/screencasts/2015-01-22-nested_forms-rails-4.2/ and http://railscasts.com/episodes/196-nested-model-form-part-1
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.