Rail upgrade with AJAX

I am new to using AJAX in Rails (I know ... I know ...), and I am sure that I am doing something wrong, maybe just a wrong call or something else. But I hunted and cannot find him.

In any case, I quickly render (this works correctly), and then I want to update it with an AJAX call. Here is what I thought would work:

<div id="two_on_two"><%= render :partial => "quickread" %></div> <p><%=link_to_remote "load two new stories", :url => {:partial => 'quickread'}, :update => 'two_on_two' %></p> 

which just deletes the page (quick flash, and then just a blank browser window). If I switch from: partial to: the action in this AJAX call, then it dynamically loads the "codeless template" in this dvd div.

It’s clear that there’s something easy, I’m absent here, right?

Thank you very much!

+4
source share
1 answer

Read the api first:

http://apidock.com/rails/ActionView/Helpers/PrototypeHelper/link_to_remote

You must correctly specify your url (controller, action, other parameters, not partial!) And define your RJS in the controller action.

IE: index.html.erb

 <div id='two_on_two'></div> link_to_remote 'Link Name', :url => {:controller => "bar", :action => "baz"} 

bars_controller.rb

 def baz respond_to do |format| format.js{ render :update do |page| page.replace_html 'two_on_two', 'Hello world!' end} end end 

So, when you click on the "Name" link, "Hello world text" will appear in your two-display div.

+2
source

Source: https://habr.com/ru/post/1304865/


All Articles