RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

I downloaded your code and just after creating the first article I got - ActiveRecord::UnknownAttributeError (unknown attribute: article):
  app/controllers/articles_controller.rb:8:in `new'
  app/controllers/articles_controller.rb:8:in `show'
  -e:2:in `load'
  -e:2

Rails 2.3.2 Ruby 1.8.7

Avatar

It was a very nice idea! Just wanna say thank you for the information you have shared.

Avatar

You must really checkout Sammy (http://code.quirkey.com/sammy). Sammy gives all of this and much more. It can be used alongside this plugin too.

Avatar

@Ryan what is the advantage of use scrAPI? why don't just use Hpricot?

Avatar

Once again very usefull screencast. I have implemented it in my new rails website. Having few problems but I will get in fully working.

Avatar

Awesome! You solve my problem. Thank You !!!

Avatar

Crikey - could it be any harder to install ImageMagick on OS X?! I downloaded the binary release and got it working easily, but I can't get the rmagick gem to see it at all. Frustrating!

Avatar

Yeah! Thanks i have been waiting for this screencast since i saw previous screencast about AJAX pagination...

Thank you!

Avatar

Great 'Cast as always. It looks like your source code is pointing to episode 170 rather than 171.

Avatar

after I change the environment.rb and restart webrick I receive the following error

/usr/lib/ruby/1.8/rubygems.rb:149:in `activate': can't activate activesupport (>= 0, runtime), already activated activesupport-2.1.0 (Gem::Exception)

if anyone has any ideas let me know, if not I will try and return with a fix.

Avatar

As "michael" pointed out, Ryan forgot to mention that you should run "script/generate nifty_layout" to get the layout stuff.

Avatar

@18: You're right that the example in this episode doesn't make much sense for Ajax. You also lose the benefit of client-side caching since every click of the back button is a new request... but the technique is helpful when there are other things on the page that you can't afford to reload: think of the pagination on YouTube comments for example.

Avatar

Really a Nice Screencast. Just a quick doubt,
When using will_paginate is it necessary that we should keep our search parameter in session?
for example
view
<input type="text" name="q">
method
@product = Product.paginate(:all, :conditions=>["name LIKE ?", "%#{params[:q]}%"], :page=>params[:page], :per_page=>10)

it is giving me those results which are not matched with the criteria, when I am scrolling through pages.. any help?

Avatar

why don't these mov files play in vlc ? I hate quicktime

Avatar

@Sig: I found nice solution to use Polymorphic Association to more than 1 lvl url: http://pastie.org/588592

Greetz, Rails rocks ;)

Avatar

Nice episode, i always read parts of it if i'm stuck at something.

I just wonder how to do it with UJS (lowpro) in a clean way.

Avatar

I love all your episodes, I can't describe the quality of them with words, thank you!

Avatar

Hi Ryan,
Thanks so much for your reply. I must say it's quite an ego boost for me knowing that your thinking matched mine :) Many thanks for your screencasts and your work on rails in general.

@Stan - I think this is a very cool technique - I consider pagination just an example. (Indeed I'm not using it as such.) Just look at what gmail achieves with it.

Avatar

Great screencast Ryan! Are there still any plans to support habtm relations since the last time it was suggested?

Avatar

You could use a dynamically generated script tag to get the pagination data. This is cleaner in IE where using XmlHTTPRequest triggers an Active-X warning.

Avatar

Guess I'm an aging Luddite, but what is the advantage of using AJAX here? All the important page content is reloaded by a trip to the server anyway, and you have to manually (well, using the utils plugin) handle all the page history stuff that ordinary page requests do automatically. What does this relatively complicated alternative win you?

Avatar

@Murdoch, nice abstract solution. I think the full URL in the hash is a little ugly, but the simplicity is great. Also you should remove the jQuery.get call on line 3.

@Goldy, unfortunately I don't know of a good way to avoid the double page params. You could use javascript to remove the "?page=2" portion but that will cause a page reload.

@Nils, the fragment (#page=x) does not get sent to the server, so there's no way we could parse it in the controller. I don't know of a solution here unfortunately.

Avatar

The following scenario concerns me.

Person A has JavaScript enabled.
Person B has JavaScript disabled.

A browses the pagination and tells B to enter the same url.

B will end up on the first page with no notice.

The controller must be modified to redirect #page=x to ?page=x before loading the page. Right?

Avatar

Wow, just fought a strange bug -- this is a real gotcha. If you are using the ":disable_with" option -- like this ...

<%= f.submit "Create review", :disable_with => translate('general.disable_with') %>

-- then the form will post as standard html. That took about an hour to find. Hope it didn't get you too.

Avatar

Ryan, here's a scenario that is be a bit ugly.

Say someone opens page 2 in a new window, thus having the url:

/products?page=2

Then if they click on the page 3 link they have the url

/products?page=2#page=3

Ewwww. In this case the likelihood of this happening is low, but in my app it's pretty likely.

Any ideas to avoid this?

Avatar

The last episodes were instrumental for me in somewhat abstracting the episode code to put ajax requests on all links between an .ajax_frame tag without explicitly specifying the params to pass:

http://pastie.org/586433

Or are there any problems with it resp. cases where ajax links are not working? :-)

Avatar

Its a big arm pump finally getting the hang of all this. Thanks for the cast, as well as the recipe entry about this!

Just to the poster before me (as I was doing it with selects), here is a good tutorial.

check out http://guides.rubyonrails.org/form_helpers.html#making-select-boxes-with-ease

Avatar

Incredible episode, thanks!

Avatar

Ryan, thanks for another great screencast. Although not specifically about ruby or rails, javascript is certainly something we all do and could get better at.

Your solution in jQuery is great; really simple and extremely readable.

For the both of us that use Mootools, this looks like a good solution that will follow the same principles described in this screencast.

http://digitarald.de/project/history-manager/

Avatar

@Seth, I do primarily use jQuery, but sometimes I will still use Prototype depending on what I'm doing.

@Fredd, I think it's good to learn both libraries and see which you feel most comfortable with. As Millisami mentioned you can use jRails to add helper methods for jQuery, but I prefer to stick with the unobtrusive approach in jQuery.

Also, the javascript helpers in Rails 3 will be unobtrusive and library agnostic (so it will work with jQuery).

@Jeff, IIRC changing the url through javascript triggers a page reload, so that is why you can only change the fragment.

Avatar

Thanks for a useful series Ryan. One question though: why not rewrite the URL to just include the query string rather than a fragment? It seems that would eliminate the need to have an event listener for refresh/bookmarking.

Avatar

Firstly, thanks Ryan for the part-2.

@Fredd, you could use jRails to make the helpers work with jquery seamlessly.

Avatar

To answer my own question (comment #33) you have to add the javascript respond_to type in your controller.

The products controller from this episode does not have any respond_to blocks. Is this common practice now to remove the respond_to blocks from controllers?

Thanks again to Ryan for such great screencasts.

Avatar

The question now is.. Should I migrate from prototype to jquery? jquery shows a lot of progress these days but the ajax helpers in Rails are for prototype:(

Avatar

Phew! I just finished watching all 175 episodes (took me a couple of weeks) and now I'm up to speed. Ryan, it's fantastic that you make such a terrific resource available to us for free -- it's much appreciated!

Avatar

@vesters it's something like: 'pageTracker._trackPageview('/pageNameHere');' pageNameHere can be nested with slashes and can be any identifier/label you want.

Avatar

Thanks Ryan. Great cast as always.

This looks like a very useful bit of kit for queue processing.

I think it might have paid to mention how often queue processing logic gets complicated (no mater how stable of scalable the technical implication of the queue processor is). It just does in the real world.

I think it would have been good to talk at least a little about the best way to execute the queue processor script itself.

While the daemon approach will probably give you slicker results, there is quite a bit of hidden overhead with daemons on production systems.

 - they HAVE to be running. There has to be a monitoring test to make sure they're running.
 - they must gracefully start and stop with the system. We have to be sure that this is going to work.
 - nice if they handle non-graceful shutdown of server (i.e. power failure). Often there is a lockfile/pidfile somewhere that may prevent this.

We all do a lot of web development where this side of a server's BAU behaviour is hidden behind tried and tested daemon applications (Apache, MySQL, MTAs) that take care of all of this for us.

Although a little more primative, a cron job might be a better option for those who are not ready to be writing init scripts on a production server.

Thanks

Avatar

@vesters yes it is
search for "track javascript events google analytics" you can even track flash events

Avatar

As always very cool!

One question you or others may know the answer to:

Is it possible to let tools like 'Google analytics' to pick up on the traffic the pages get, when it's all done in JS?

Avatar

great screencast again! very clean solution. briliant!

Avatar

Looks great, thanks. Do you use jquery to the exclusion of protoype now?
sc

Avatar

Hi Ryan, great episode! However i think there is one, much more elegent solution for handling ajax request, called Taconite. My friend described it here: http://codetunes.com/2008/12/08/rails-ajax-and-jquery/
Cheers, hope you like it.

Avatar

This is my favorite cast so far.

Avatar

If you try it on Windows and get an error related to "libtidy.so", just delete the libtidy.so file in folder "ruby/lib/ruby/gems/1.8/gems/scrapi-1.2.0/lib/tidy". This will force scrapi to use "libtidy.dll" in the same folder...

Avatar

Hi everyone,

I've implemented a plugin called dependent_select on

http://github.com/splendeo/dependent_select/tree/master

The syntax is very similar to collection_select - you just add another method for "filtering".

<%= f.dependent_collection_select :state_id, State.find(:all), :id, :name, :country_id, :prompt => "Select a State" %>

This will generate all the needed javascript - with the exception of the main function, that should be included on the layout.

Note that the current implementation downloads everything at the begining on a big javascript array; if you are looking for an AJAX solution you'll have to look elsewhere - for now :) at least.

Regards!

Avatar

Another great Railscast, and oddly exactly at the time I needed to know this!

Avatar

Wookie! ha!

This was just the Railscast that I needed---I was going to implement I18N in a different way. Short and right to the point. Thanks, Ryan!

Avatar

When doing java development I used to use flying_saucer. In my last app I used jruby along with wycats-ufo for seamless ruby integration with flying saucer. It's a dead simple 1 method call to make a pdf. I would recommend it to anyone that would consider running on jruby. At least even a small service to just generate the pdf's.