Tôi đang theo hướng dẫn: http://www.amooma.de/screencasts/2015-01-22-nested_forms-rails-4.2/Rails hình thức lồng lỗi, con phải tồn tại
Tôi Rails usign 5.0.0.1
Nhưng khi tôi đăng ký một khách sạn, có vẻ như các loại khách sạn phải tồn tại .
1 lỗi bị cấm khách sạn này được cứu: Categories khách sạn phải tồn tại
mô hình khách sạn của tôi:
class Hotel < ApplicationRecord
has_many :categories, dependent: :destroy
validates :name, presence: true
accepts_nested_attributes_for :categories, reject_if: proc { |attributes| attributes['name'].blank? }, allow_destroy: true
end
My Thể loại mô hình:
class Category < ApplicationRecord
belongs_to :hotel
validates :name, presence: true
end
My hotel bộ điều khiển:
def new
@hotel = Hotel.new
@hotel.categories.build
end
def hotel_params
params.require(:hotel).permit(:name, categories_attributes: [ :id,:name])
end
End _form.html.erb tôi
Thanks for the help, tôi thấy rằng 'inverse_of :: categories' cũng làm việc. –
Cảm ơn Dharam, điều đó đã được giúp đỡ. Ngoài ra, lưu ý rằng 'required: false' không được dùng nữa (nguồn: https://github.com/rails/rails/pull/18937). Sử dụng tốt hơn 'thuộc_to: khách sạn, tùy chọn: true' – htaidirt