daze
2010-Nov-20 22:59 UTC
How to get the :remote => true working to update a part of the page?
In Rails 2, I used this kind of code to get a certain part of the page
to update.
<!-- section/show-->
<% if @section.has_sidebar %>
<div id=''sidebar_div'' class=''span-3''>
<ul class=''sidebar_list''>
<% for page in @section.pages %>
<li><%= link_to_remote "#{page.name}",
:update=>''show_page'', :url
=>
{
:controller=>''pages'',:action=>''show_page'',
:id=>page.id }, :html=>{}
%></li>
<hr/>
<% end %>
</ul>
</div>
<% end %>
<div id=''show_page'' <% if @section.has_sidebar
%>class=''span-18 last
vert_divider''<% end %>>
<%= render :partial => ''pages/show'', :locals
=>
{ :page=>@page } %>
</div>
-----
In the controller :pages, I had a show_page method that just rendered
the partial again.
In Rails 3, I don''t know how to accomplish this. I want to use
jQuery, which I already know how to code, but I don''t know how to get
it working in this way. Should I create a js.erb file in my view? I
watched the http://railscasts.com/episodes/205-unobtrusive-javascript
but I still don''t know how to get it working to update one part of the
page! Basically, I have this sidebar with links on it, and each time a
user a clicks on one of them, the central part of the page should
update - AJAX! How do I do this? Do I still need this show_page
method in the controller?
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
daze
2010-Nov-21 03:09 UTC
Re: How to get the :remote => true working to update a part of the page?
I found a clean answer, and I thought I''d explain the solution.
Basically, I was too caught up in the jQuery and etc. - in this case,
Prototype works very nicely. With jQuery, I got to fumbling around
with a js.erb files. With prototype, in this, case the solution is
really clean.
http://www.ruby-forum.com/topic/211467 describes a similar solution.
---The View---
<!-- section/show -->
<% if @section.has_sidebar %>
<div id=''sidebar_div'' class=''span-3''>
<ul class=''sidebar_list''>
<% for page in @section.pages %>
<li><%= link_to "#{page.name}", page, :remote => true
%></li>
<hr/>
<% end %>
</ul>
</div>
<% end %>
<div id=''show_page'' <% if @section.has_sidebar
%>class=''span-18 last
vert_divider''<% end %>>
<%= render :partial => ''pages/show_page'', :locals =>
{ :page=>@page }
%>
</div>
---PagesController---
class PagesController < ApplicationController
def show
@page = Page.find(params[:id])
respond_to do |format|
format.js {
render(:update) do |page| # uses Prototype, the JS framework
page.replace_html ''show_page'', :partial =>
"show_page", :locals
=> { :page => @page }
end
}
end
end
end
Hopefully this will help anyone who''s encountered this same problem,
so that person can come to the answer more quickly. :D
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.