b.herring
b.herring

Reputation: 653

how to call javascript from a rails controller action

I have a form and on the submit, it directs to a 'overlap_check' controller action. In the action, if a condition is met from the params that have been sent, i want to render a modal on the page by calling a javascript function. Is this possible?

I've seen some examples using:

# render :js => "('#testing').modal('show');"

But no luck with this.

thanks

Upvotes: 2

Views: 1608

Answers (2)

kishore cheruku
kishore cheruku

Reputation: 521

Please mention below format in controller action.

respond_to do |format|
        format.js 
end

And create actioname.js.erb in views. There you can call ('#testing').modal('show'); . If you calling controller action by link then add remote: true

<%= link_to your_link_name, path, :remote => true %>

Or If you calling from action then add remote: true in your form

Then it should be work.

Upvotes: 0

demir
demir

Reputation: 4709

1) in controller:

def overlap_check
  respond_to do |format|
    format.js
  end
end

2) create overlap_check.js.erb

3) add js code to overlap_check.js.erb

('#testing').modal('show');

4) add remote: true to form

Upvotes: 2

Related Questions