[Rails] Nested form - ajax call

59 views
Skip to first unread message

*** Mateus ***

unread,
Mar 15, 2014, 10:33:11 PM3/15/14
to rubyonra...@googlegroups.com
Hey guys,

I was wondering what would be the best practice/approach to send ajax request from inside a nested form?
Lemme try to describe it better:

= form_for [@customer, @invoice] do |invoice_form|
  
  = invoice_form.label :date_issue
  = invoice_form.text_field :date_issue, :class => 'datepicker'
  
  = invoice_form.fields_for :item_services do |item_fields|
    = item_service.label 'Search for services'
    = item_service.text_field :description

    = item_service.submit 'Search'

= invoice_form.submit 'Create Invoice'

The outer form is the last to be submitted(creates an Invoice) and in the meanwhile, in the inner form, I'd like to search for an Service and if it doesn't exists, I'd be rendering some field to create a service and then keep going on the flow.

This is a many-to-many relationship
Invoice ->|--------------|<- ItemService

Invoice
has_and_belongs_to_many :item_services
ItemService
has_and_belongs_to_many :invoices


Thanks in advance

Brad Bollenbach

unread,
Mar 17, 2014, 4:39:52 PM3/17/14
to rubyonra...@googlegroups.com
*** Mateus *** wrote in post #1140030:

[...]

> The outer form is the last to be submitted(creates an Invoice) and in
> the
> meanwhile, in the inner form, I'd like to search for an Service and if
> it
> doesn't exists, I'd be rendering some field to create a service and then
> keep going on the flow.
>
> This is a many-to-many relationship
> *Invoice* ->|--------------|<- *ItemService*
>
> *Invoice* has_and_belongs_to_many
> *:item_servicesItemService* has_and_belongs_to_many *:invoices*


Hi Mateus,

It sounds like you want an "autocomplete" like functionality for that
field, right? If so, I'd recommend using the jquery autocomplete widget:

http://jqueryui.com/autocomplete/

There are Rails wrappers for this here (depending on what version you're
on):

https://github.com/crowdint/rails3-jquery-autocomplete
https://github.com/peterwillcn/rails4-autocomplete

Does that help?

--
Posted via http://www.ruby-forum.com/.
Reply all
Reply to author
Forward
0 new messages