views:

247

answers:

1

I'm new to using AJAX in Rails (I know... I know...), and am positive that I'm doing something wrong that's probably just the wrong call or something. But I've hunted and can't seem to find it.

Anyway, I'm rendering out quick partial (this is working right) and then I want to refresh it with an AJAX call. Here's 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>

that just blows out the page (a quick flash and then just a blank browser window). If I switch from :partial to :action in that AJAX call, then it dynamically loads a "template missing" in that two_on_two div.

Clearly, there's something easy I'm missing here, right?

Thank you so much!

+1  A: 

First, read api:

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

You should specify your url correctly (controller, action, other params, not partial!) and define your RJS in 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 will click Link Name, in your two_on_two div will appear "Hello world text".

fl00r
I can load text or a partial using your method, but I can also do this: def loader render :partial => "quickread" endHowever, I can't figure out how to pass a variable into that partial.
Dan Sinker
use `render :partial => "quickread", :locals => { blabla => @blabla }` if blabla would be the name of the variable :)
nathanvda