2

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

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

I'm trying to do something similar in Rails 7, it seems like this approach no longer works for Rails 7.
Yes, this approach doesn't work in Rails 7. Turbo stream can be used instead.
0

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.

5 Comments

thanks, i've done this, however im getting an ActionController::UnknownFormat error?
how you calling your controller action, By clicking link? or by form submit?
i call the controller with form submit
Then add remote: true in your form. still if you have doubt then send me your form once.
I hope you got solution :) . Happy coding

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.