问题
I have the following models and relations:
Rate fields
t.string :type
t.string :name
class Rate < ActiveRecord::Base
has_many :category_rate_requests
end
CategoryRateRequests fields
t.date :date_from
t.date :date_to
class CategoryRateRequests < ActiveRecord::Base
belongs_to :rate
has_many :category_rates
end
CategoryRate
t.integer :room_category_id
t.integer :new_rate_id
t.integer :category_rate_request_id
t.integer :amount
class CategoryRate < ActiveRecord::Base
belongs_to :rate
belongs_to :category_rate_request
belongs_to :room_category
end
And I'm trying to have a nested_form inside a nested_form
= nested_form_for @rate do |f|
= label_tag :name, t('rates.new.name'), class: 'grey h2'
= f.text_field(:name, required: true, class: 'form-input form-control full-width-input')
= f.fields_for :category_rate_request do |request|
= request.text_field(:date_from, class: 'date-input form-control start-date-input', type: 'text', 'data-provide': 'datepicker', placeholder: t('common.date_from'))
= request.text_field(:date_to, class: 'date-input form-control end-date-input', type: 'text', 'data-provide': 'datepicker', placeholder: t('common.date_to'))
= request.fields_for :category_rate, do |u|
= u.number_field(:price, class: "form-control", placeholder: placeholder)
Overall this is the format. It has more things, but to not include too much unnecesary information I omitted some fields of the form.
But I get the following error
Completed 500 Internal Server Error in 690ms (ActiveRecord: 6.1ms)
SyntaxError - syntax error, unexpected keyword_do_block ; _slim_controls2 = request.fields_for :new_category_rate, do |u|; ^ /project/app/views/new_rates/_category_rate_requests.html.slim:61: syntax error, unexpected keyword_ensure, expecting end-of-input:
That line marked in red is:
= request.fields_for :new_category_rate, do |u|
Is there any way I can have the attributes of all three models in one form? In the Rate
form more precisely.
回答1:
(copied from comment)
You have an extra comma in the erroring line. Change it to:
= request.fields_for :new_category_rate do |u|
回答2:
I don't know if you need to use that gem for a particular reason, but I think it hasn't been updated for too long for use it.
I use Cocoon, and with simple form you just need to do:
simple_form_for @a do |f|
f.simple_fields_for :b do |b_f|
b_f.simple_fields_for :c do |c_f|
...
end
end
end
You can use link_to_add_association
and link_to_remove_association
in a very friendly way too.
来源:https://stackoverflow.com/questions/38977301/rails-nested-form-inside-nested-form