RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

Victor Moroz: I'm probably going to be crucified for saying this, but I no longer care about the 0.5% (or less) weirdos who have JavaScript disabled.

Avatar

Hi Ryan, I agree with the other comments above. You can use a cookie to store the username and the role, to avoid to hit the users controller.

Of course this technique doesn't expose the app to any security hole, because of the server-side check when an administrative link will be clicked.

@Duncan here a snippet (and a related blog post too) for parse cookies with Prototype: http://bit.ly/yPStP

Avatar

Why not use fragment caching for the central part (parts) and generate layout (with header) each time the page is accessed? Not much difference in speed, but it might be cleaner. The only exceptions are Edit / Destroy links, which can be added through JS. This way is also unobtrusive (except admin links, which are not so important for unobtrusiveness).

Avatar

Chris: Looks like a good example. But you can set the cookie through JavaScript/jQuery as well. Even less work for the server.

Avatar

Pawel: My thoughts exactly. People, wake up. Put your presentation logic on the client-side -- yes, that means JavaScript.

I wonder how much longer it will take the majority of Rails programmers to realize this.

Avatar

Aaah, Pastie didn't work like I'd thought. Well, I'll post it inline. It goes:

(in application_controller)
before_filter :set_user_info_cookie

private

def set_user_info_cookie
  cookies[:user_info] = { :value => current_user.email, :expires => 1.hour.from_now } if current_user
  cookies.delete :user_info unless current_user
end

jQuery snippet:
if ($.cookie('user_info')) {
  $("p#account").html($.cookie('user_info') + " | <a href='/myaccount'>My Account</a> | <a href='/logout'>Logout</a>");
};

Avatar

Duncan - I got this working in my Rails app (using Authlogic). It basically looks like this:

(trying out Pastie, let's see if it works...)

In application_controller.rb:

<script src='http://pastie.org/535397.js'></script>

Then, in my site javascript file, I use the last part of this jQuery cookie plugin (the first two-thirds are for writing cookies with jQuery, which we won't need):

http://plugins.jquery.com/project/Cookie

And then:

<script src='http://pastie.org/535398.js'></script>

Now, whenever you're logged in, jQuery seeks out the p tag with an id of "account" and fills it with the user's information.

Avatar

If you store username in cookie on login, and clear it on logout, you can avoid hitting Rails. Javascript would then use this cookie to hide or display links on the page. This way all can be done entirely client side.

Avatar

Thanks for another great screencast, Ryan!

Have you thought about using the session cookie do determine whether a user is signed in? This way a static javascript file could optionally enable the admin links and a request to users/show.js would be eliminated. I'm currently trying to implement this on one of my projects, but there are two issues:

1. The rails session cookie is difficult to decode and parse using javascript.
2. The application could set a cookie on login for this purpose, however I'm still figuring out how to integrate this with Authlogic.

If I make any progress, I'll share it here.

Avatar

Great idea and great screencast.

First I thought that it is not a good Idea to only hide the links, because "FireBug"-Users will find them, but as you can restrict the execution of these (often restful) actions it is not that big problem, if someone knows them.

Avatar

Out of curiosity, I'm also interested in seeing how "OR" logic could be implemented here.

This is actually quite clean given countless alternatives! ;)

Avatar

I found that this plugin works very well for any xml, but the documentation is not available. Just do the following:
- freeze the feedzirra gem
- in the parser directory clone the rss and rss_entry files, rename them and change the mappings to match you xml structure
- locate feedzirra.rb and add references to your new parser files
- in feed.rb update @feed_classes array with your new parser

In general you should be able to use the example files as reference. Also make sure to update the specs.

Avatar

+ eight gazzilion billon for recurring billing, Ryan this !#*$ is the bomb.

Avatar

As always awesome reailscast.
I'm going into Rspec+Cucumber and so on right now and those last episodes are really useful.
Hope you are going to make some more on the topic.

Have a good 1 buddy.

Avatar

Someone please help urgently,
I added a patch for the plugin selenium_on_rails for rails 2.2.2 from Selenium test runner and simple test cases working fine.

But method include_partial is not working in my rselenese test case
include_partial 'login', :name => 'user1', :password => 'password'

error
 NoMethodError in Selenium#test_file

Showing test/selenium/login/invalid_login_test.rsel where line #1 raised:

private method `split' called for #<Hash:0x7ea9708>

Avatar

I am really looking for a JQuery solution for this as the current code as is, is not working properly.

I do not know JQuery much so any help in fixing this will be helpful.

Avatar

Your screencast for dynamic select menus helped me. Thx for sharing it!

Avatar

I am trying to require factories but i keep getting this message.

no such file to load -- /Volumes/Work/visbid/spec/factories

any help? thanks

Avatar

Is anyone of you able to send a email contending html, plain and attachment? (all 3 parts in 1 email)
and if so, how?

Avatar

Great video as always.

I'm having trouble getting the crontab to write the crontab with a Capistrano deploy to EC2. While it's deploying it tells me that the crontab has been written, but when I log in and run crontab -it's still the old crontab (previous Capistrano release). I have to then manually run whenever --write-crontab. And then running crontab -l shows the correct release.

Does anybody have any ideas on what could be causing this? I have tried a million variations and nothing seems to be working.

Avatar

Hey Ryan, I failed to install metric_fu ,since I am using windows system it is giving me problem for nmake (it is a shell command windows not able to recognize it), The same problem occurred to me when installing relevance-rcov for cucumber.

Avatar

@ryan what problems were you having with updating? I cant seem to get it to work either. It fetches and parses fine. Anyone else getting this?

http://pastie.org/530375

Avatar

Karim:
sorry if this is not what you are looking for but what about:

def find_by_tag name
Tag.find_by_name(name).articles
end

Avatar

Although Windows does not have Cron one might have some luck using Vixie cron which can be installed in Cygwin. I have had partial success using Vixie cron on a Windows system thought it wasn't a breeze.

http://www.cygwin.com/

Avatar

Is there anyway to get this to work with ajax, because i need to load 4000 cities. I have searched every where and found nothing. Please Help

Avatar

I'm getting errors installing it on a Windows box because of curl. Is there a way to install curl on windows?

Avatar

This is awesome .. I had to do this a couple weeks ago.. I had the same moving parts as you but not as clear and concise!! Thanks for totally showing me the right way to do this!
I agree with Nils.. when I first saw the title I had deja vu!

Avatar

Hi
    Thanks Ryan for the screencast.I have the problem with autosizing the cell My table has three columns And if the third cell content is very big, suppose a lengthy email then the content overflows from cell to right margin of pdf Could you please tel how can I control this? My example code is
[
  c.full_name,
  'sales admin, purchase admin, stores controller,store keeper,sales admin, purchase admin', 'test.....@test.com']

pdf.table contact_data,
 :border_color => "DDDDDD",
 :headers => ["Name", "Roles","Email"],
    :border_width => 3,
    :align => { 0 => :left, 1 => :left, 2 => :left }

Avatar

Anybody dealing with RSS parsing for a lot of feeds and who don't want to have the hassle of polling should check out http://superfeedr.com

Let me know what you think! (You included Ryan ;))

Avatar

I had a small issue when indexing from the cron job, it wouldn't reindex new records, but it worked fine when manually running the command that was in the cron file.

The solution was to add the location of the sphinx binaries to sphinx.yml (this only applies to passenger apps) http://freelancing-god.github.com/ts/en/common_issues.html.

Hope I save someone a couple of hours :)

Avatar

Great! Thanks for this Ryan

ps - I think feedzirra is supposed to rhyme with Godzilla... it's a joke

Avatar

I second the notion that Ryan has ESP. His screencast's always seem to be right on target with what I am working on. We joke that he has bugged our office space :D.
Thanks Ryan another great episode.

Avatar

Thank you Ryan!
That's exactly what I needed!
So far, I used Nokogiri. The performance of XML processing is really better than Nokogiri and than Hpricot?

Avatar

It is starting to become kinda creepy … it's as if you were reading my mind. Always coming with stuff I need just in time!

Love your screencasts!

Avatar

Thanks Ryan

any idea how to combine multiple feeds into one master feed?

Avatar

Been using FeedZirra for some time now. It's been really great. But long/complex urls on Ruby Enterprise Edition have caused problems recently.

Avatar

@Nicklas, a better way to test your helper object would be to create a mock object instead of an actual ActionView::Base object, and test that the mock is called properly. No need to test ActionView--it's got a rich set of tests of its own!

Avatar

@Nicklas, I just had the same question and passing ActionView::Base.new as the template worked for me!

Good luck!

Avatar

Thanks, Ryan. Very interesting episode (as usual). You're saving me a lot of time searching new rails gems/plugins.

Avatar

Is it possible to make a controller that displays a Index of tags which you can click on? The Show view would then show the Articles that belong to the tag.

Avatar

Thanks for this! Great help.

I found myself in one application I am working on repeating the page title in the title block but also in the h1 element.

So I modified it slightly to help me with this: http://pastie.org/527089

Avatar

Well i think i might have found a way.. I've created a named_scope in the Articles model:

named_scope :find_by_tag, lambda { |*args| {:include => :tags, :conditions => ["tags.name ILIKE ?", '%'+args.first+'%']} }

Not sure if it's the best way (probably not) but seems to work... any better ideas?

Avatar

Man I love this site. Every single screencast has somehow been included in a project i'm working on, and this one is no exception :)

Now taking this further, how would i go about doing a Article.find_by_tag_name method or named_scope?

Any help would be greatly appreciated.

Thx.

Avatar

@AcaKaliman

application.rb has since been renamed application_controller.rb. Rename this file and try it again. It was successful for me.

Avatar

Hi!
I like that stylesheet. Is it made by you and can I use it for my testing?

Keep up with the good work!
Felix

Avatar

Alex Reisner, take a look at "Session bugs".

http://rdoc.info/projects/binarylogic/authlogic

It's because of Passenger. Bring your sessions into database, and then everything will work fine!

Hope it helps

Avatar

Just to echo what others have said, after multiple searches across the web this solved my check_box problem in five minutes. Thanks so much!

Avatar

Nitin, can you clarify where the opensection.id is defined? Is it part of 'ProposalSections' or 'Proposals'?
Thanks.
This may be just what I need to solve a similar membership type challenge i'm having with my view.

Avatar

Thanks for the screencast Ryan, I just implemented TS last week.