A
A
Andrey Baibek2019-03-03 19:33:25
HTML
Andrey Baibek, 2019-03-03 19:33:25

How to make ajax render js response in Ruby On Rails?

There is a div element, when I click on it, an ajax request to the controller is triggered, which in turn should return a response in js format (replace the contents of the block). As a result, he returns the answer to me with html markup. And I want the behavior of the link_to helper, only in my case with a div, so that I click on the block, then the controller call, then the response in js format, which is to change the text inside the block. Do not tell me how to do everything right?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Mirilaczvili, 2019-03-03
@AndreyBuyback

You need to use the helper like this: In the controller of the corresponding action:

respond_to do |format|
      format.js   { render :layout => false}
    end

https://guides.rubyonrails.org/working_with_javasc...
https://blog.codeship.com/unobtrusive-javascript-v...

A
Andrey Baibek, 2019-03-04
@AndreyBuyback

Thanks for answers. How does format.js work? Renders a certain .js file in the view and gives the generated page to the client, or does it give the javascript code that is in the view to the client, and he executes it on his side?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question