K
K
kramidev2015-02-19 13:15:06
JavaScript
kramidev, 2015-02-19 13:15:06

Ajax in rails. How to do view more?

Hi all. How to do get else in rails using ajax?

there is a link

<%= link_to main_index_path(:countNews => @news.count + 9), remote: true do %>
               Больше новостей
            <% end %>


As far as I understand remote declares ajax. And then I don’t understand the request goes to the controller or does it need to be processed?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Viktor Vsk, 2015-02-19
@kramidev

The request goes to the controller. It needs to be processed.
In order for all these remote: true to work correctly, a library must be connected, for example, with jquery -
//=require jquery
//=require jquery_ujs
Then the magical behavior of all these attributes will appear.
After that, the request goes to the same controller as usual, but with the js format
In the simplest case, you just need to add a view, in your case:
And write a javascript code in it that will implement "show more". For example, insert (append) received variables (new news from the controller).
If the logic for html and js formats is different, then write in the action

# news_controllerb.rb
def index
#common format code
@news = News.all
respond_to do |format|
format.html{ #html specific code }
format.js{ #js specific code }
end
end

But yours is
confusing. It will be necessary to implement some kind of mechanism so that not the same news is given further, but constantly - the following.
UPD :
You need to check requests using pry or byebug gems. Put a breakpoint in the right place in the controller (where you are waiting for the request) and see what is where.
Instead of alerts, use console.log()
See what happens with the request in the browser in the Network tab

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question