Monday, November 14, 2005

ajax in menu

i figured out how to incorporate ajax into the recent posts section of the menu. i have it set to only show 5 recent posts by default. i put a link that says "more" under the five posts. when clicked, it expands the list to 20 automatically. very nice.

i probably went about it the wrong way, but hey, it works. what i did was put a link_to_remote function in the index under the recent posts list. i put the recent posts in its own div--recent_posts. here's what the code looks like:

link_to_remote( "more", :update => "recent_posts",
  :url =>{ :action => :more_cats })

so i made a function in the controller called more_cats that declares a @recent variable to find_by_sql the 20 most recent posts:

def more_cats
  @recent = Post.find_by_sql "SELECT id, title FROM posts
  ORDER BY created_on DESC LIMIT 20"
end

that worked, except for that it was using the same template and loading the whole header and footer part into the menu. i found out how there's an :except attribute that can be passed to the layout call. so all i had to do was this:

layout "posts", :except => :more_cats

now it doesn't use a layout for more_cats and it works fine.

0 Comments:

Post a Comment

<< Home