trestle icon indicating copy to clipboard operation
trestle copied to clipboard

How can I call a custom action from ajax when form input change (onchange js)

Open hdbreaker opened this issue 5 years ago • 0 comments

I have a custom action that I need to trigger to populate a select in a :new form.

I create a custom function and route that return JSON when its called:

def get_free_slots
      # some stuff here
      render json: data
    end

routes do post get_free_slots, on: :member end

I am able to trigger the action with a link_to function:

link_to "Get Free Slots", admin.path(:get_free_slots, id: 1, params: {"date": project[:start_date].to_s}), method: :post, class: "btn btn-info"

Now, I need to call get_free_slots route with AJAX, and use the retrieved JSON to populate a "select" form field.

Can you explain to me how to achieve this?

hdbreaker avatar Jun 01 '20 16:06 hdbreaker