63294

Rails nested_form inside nested_form

Question:

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

<blockquote>

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:

</blockquote>

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.

Answer1:

<em>(copied from comment)</em>

You have an extra comma in the erroring line. Change it to:

= request.fields_for :new_category_rate do |u|

Answer2:

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 <a href="https://github.com/nathanvda/cocoon" rel="nofollow">Cocoon</a>, 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.

Recommend

  • Rails nested_form inside nested_form
  • Rails 4 custom helper method with form doesn't get output in view - fully
  • Add custom class to form fields
  • Difference between rails label and label_tag
  • data-blueprint-id conflict on Nested form for polymorphic associations
  • Ruby on Rails: Find a record by an attribute not an id
  • Django generic login view return 'str object not callable' error
  • Rails 3 UJS dry Client + Server Side form validation
  • Rails ajax form not submitting from within another
  • I can't create model objects using accepts_nested_attributes_for. It won't create the nest
  • How can I have equal heights for inner elements of flexbox grid/boxes/cards without using jQuery?
  • Contact form problem - I do receive messages, but no contents (blank page)
  • How can I make this modal persistent?
  • Does Mobilefirst provide a provision to access web services directly?
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • java.lang.NoClassDefFoundError: com.parse.Parse$Configuration$Builder on below Lollipop versions
  • Nant, Vault & Windows Integrated Authentication
  • Bug in WPF DataGrid
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • what is the difference between the asp.net mvc application and asp.net web application
  • jQuery tmpl and DataLink beta
  • How can I estimate amount of memory left with calling System.gc()?
  • Matrix multiplication with MKL
  • SQL merge duplicate rows and join values that are different
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • Proper way to use connect-multiparty with express.js?
  • How to set the response of a form post action to a iframe source?
  • sending mail using smtp is too slow
  • Does armcc optimizes non-volatile variables with -O0?
  • Busy indicator not showing up in wpf window [duplicate]
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?