Obserwuję tutorialu: http://www.amooma.de/screencasts/2015-01-22-nested_forms-rails-4.2/Szyny błąd forma zagnieżdżone, dziecko musi istnieć
jestem usign Rails 5.0.0.1
Ale kiedy zarejestrować hotel, wydaje się, że musi istnieć kategoria hotelu .
1 błąd zabronione ten hotel z zapisywane: Kategorie hotel musi istnieć
Mój model Hotel:
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 Category model:
class Category < ApplicationRecord
belongs_to :hotel
validates :name, presence: true
end
Mój hotel kontroler:
def new
@hotel = Hotel.new
@hotel.categories.build
end
def hotel_params
params.require(:hotel).permit(:name, categories_attributes: [ :id,:name])
end
Koniec moja _form.html.erb
Dzięki za pomoc, widziałem, że 'inverse_of :: categories' również działa. –
Dzięki Dharam, pomogło. Zwróć też uwagę, że 'required: false' jest przestarzałe (źródło: https://github.com/rails/rails/pull/18937). Lepiej użyj 'belongs_to: hotel, opcjonalnie: true' – htaidirt