RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

Thanks Ryan! Just updated a couple of my apps. These are easy to forget and it's nice to have a solid checklist in place. One of your most important screen casts yet!
Brian

Avatar

It's so easy to forget these h methods. Looking forward to Rails 3, any one knows when it's ready?

Avatar

append the string "GIF89" to the upload file will bypassing the content_type filter,for example:

GIF89
<?php phpinfo(); ?>

Avatar

this is perfect - just what i need to do but was putting off.

Avatar

if you run mod_php its quite simple to deactivate php for a directory tree. Just create a file .htaccess in the base directory of that tree and add one line to it:

php_flag engine off

Make sure the webserver can read that file.

Avatar

Scratch that last comment...figured it out.

(1) I was trying to "login" using my OpenID credentials before "registering". Gotta register first, then you can use OpenID to login during subsequent visits to the site.

(2) For registration, I had the crypted_password and password_salt columns in the user model set to :null => false. But using OpenID, those don't get created which throws a database error. A simple migration using the following fixed that:

http://pastie.org/607719

Once I fixed that and registered for the site FIRST, it worked just fine.

Avatar

I've tried setting this up on a test site using Authlogic but keep getting the following error once I return from myopenid.com:

------
Openid identifier did not match any users in our database, have you set up your account to use OpenID?
-----

I've looked at the README for the plugin, on Google and here but I'm stumped.

Any ideas?

Thanks.

Avatar

Thanks for this screencast! By the way, I tried to modify the tests so that they check for a redirect after logging in. So I used assert_redirected_to .... But now I get an error saying there was no expected redirect and just a 200 status code. Argh!!! This sucks! I want to use assert_redirected_to but I can't because something gets broke when you mix Webrat with Rspec!!! Any ideas??

Avatar

I wonder how this should work:
UserSession.find
as find needs an ID, at least I always get "Couldn't find UserSession without an ID" if I try that. UserSession.first works though (returns nil in case no session was found)

?

Avatar

1) Big thanks for Your screencasts.
2)Why dont just use $("#products").load(this.href) and in view just render list of products ? You dont have to use jqeury to put generated html into proucts div.
Or maybe You have to. I was playing with this episode on CakePHP not on Rails.

Avatar

@Tim Gossett - You need to post what you said about getting will_paginate to work with searchlogic in the forums. This is the only place I found that solution... even though it's a simple easy one some people don't think of doing that. Thanks sooo much!!

Avatar

Ok I'd like to do this but I don't want to create a new project.

Here's what I have...
a User has_many References

The applicant is already created ahead of time. But how do I create that array... I feel like this should be simple but I'm having a hard time with it.

References Controller: http://pastie.org/605600
New References view: http://pastie.org/605604

I'm pretty sure my view isn't right... still learning...

Should I be doing this from the User controller or the References controller? I didn't think I would do this out of the User controller because I'm not creating a new user just new references.

Any thoughts? Thanks.

Avatar

Thanks, this helped tremendously.

Because of my total lack of knowledge about symlinks and Linux in general, I was a bit confused about something there.

In this example:

run "ln -nfs #{shared_path}/assets #{release_path}/public/assets"

the latter path links *to* the former, right? I.e. you create a symlink *from* #{release_path}/public/assets *to* #{shared_path}/assets

So, anything being saved to /public/assets actually ends up in the shared/assets folder.

Please tell me if I'm wrong.

Avatar

Great cast Ryan! I have gone on to allow searching over multiple indexes. Also due to the app Im working on have written the sql_query myself, if you do this you have to re-index:

rake thinking_sphinx:index INDEX_ONLY=true

Not to over write your custom sql!

Avatar

Can touch be used in a polymorphic relationship?

Avatar

how can I define dafault values for task, for example

Avatar

I was using acts_as_versioned. Thanks Ryan. This one is good.
@Bryan, Thanks. I got the HTMLDiff working as well. Its fantastic.

Avatar

At the end of the video Ryan mentions the form_for method should not be used for public facing pages. He says to setup custom forms in those situations. What exactly is meant by that? Does that mean to use form_tag to build the form? If so, how do I do this using multiple scopes?

Avatar

If you want to offer your users a simple search box instead of a complex form to search your models, check out scoped_search: http://github.com/wvanbergen/scoped_search

class Model < ActiveRecord::Base
  scoped_search :on =>[:name, :description, :created]
end

Model.search_for(params[:q]).paginate(...)

This will work with queries like:
- keyword1 keyword2 keyword3
- (a | b) & created > 2009-01-01
- name = "Willem van Bergen"

Avatar

I have the same error of sravanthi, when I call 127.0.0.1:3000/makepdf/13.pdf I get this: Missing template makepdf/show.erb . In MakepdfController I have
def show
@asset = Asset.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @asset }
format.pdf { render :layout => false }
end
end
any hint?

Avatar

For getting the log and the hirb working, install the hirb gem and put

if ENV['RAILS_ENV']
  require 'rubygems'
  require 'hirb'
  require 'activerecord'
  Hirb.enable
  ActiveRecord::Base.logger = Logger.new(STDOUT)
end

into .irbrc and then fire up script/console

Avatar

What version of Rails does vestal_versions require, will it work on 2.1?

Avatar

I've been using acts_as_revisable on my project, which has worked out extremely well, it seems to be a bit more developed, and works very will in conjunction with AASM. acts_as_revisable also supports association versioning as detailed in the without_scope blog: http://withoutscope.com/2009/5/12/simple-association-versioning-with-acts_as_revisable

http://github.com/rich/acts_as_revisable/tree/master

Avatar

oops; nevermind, my mistake. Just forget I mentioned it. (my issue was actually the user.time_zone field in the database)

Avatar

Ryan,
As usual great screencast. I borrowed from this teaching some time ago, however, w/Rails 2.3.2 time_zone is no longer a defined method (e.g. NoMethodError (undefined method `time_zone')
...and, thus far, I have wasted 30 minutes trying to find it's equivalent.

Thanks in advance for any pointers,
Jonathan

Avatar

@sam
Event shorter: Ryan made an episode about that: http://railscasts.com/episodes/6-shortcut-blocks-with-symbol-to-proc

In short: its just a shortcut for
Page.find_each{|item| item.touche}

The & converts the symbol to a proc object ( to be more specific the & calls the to_proc method on the symbol or on any other object), which gets then called for each object which find_each returns.

Thats feature is in ruby 1.9 per default, or you can define it like rails did.

Look at http://blog.hasmanythrough.com/2006/3/7/symbol-to-proc-shorthand and search for to_proc
 

Avatar

I looked at the code and i believe that the method which is responsible for processing the "or" conditions, don't get called if you use the User.search ... form.

But I am not sure.

I am still learning the "meta-programming" thing in ruby. But I believe Dave Thomas or Paolo Perrotta sad there is no such thing, its just programming ;-)

I don't have a github account yet, but can somebody post there a comment about the searchlogic issue.

@ryan Your videos are great. They help me to forget php ;-)

Avatar

@hartwig,

I have the same issue. Any ideas anyone? Maybe we're looking at a gem conflict?

Avatar

Hi, very nice screencast

can someone explain me what the & in

Page.find_each(&:touche)

does?

thx

Avatar

works great but one problem - my model has a "state" column that gets changed from "pending" to "active". this would allow users to revert an active post to pending - is there a way to prevent Vestal Versions from not tracking a specific column (in this case the "state" column)? thanks!

Avatar

Instead of printing a loading messages every time you make an AJAX call, you can use something like this:

http://docs.jquery.com/Ajax_Events

Avatar

Anyone wondering about the name of the vestal_versions plugin probably needs to listen to A Whiter Shade of Pale

http://www.youtube.com/watch?v=PbWULu5_nXI

Avatar

NOTE:
using current_user helper in the layout will hit the database and fetch the user record for every request from logged in user.

Use It Wisely!

Avatar

BTW as of today with rails 2.3.2 the session domain thing has apparently changed.

Now it should be ActionController::Base.session_options[:domain] = '.blog.local'

and not [:session_domain]

Thx for the cast!

Avatar

@Shane
has_versioning plugin ( http://github.com/kkurach/has_versioning/tree/master ) doesn't support rollbacks yet. everything else you wrote is correct :)

it supports tracking objects AND associations, and also packing many changes into one changelist

Avatar

Forgot the link:

http://github.com/airblade/paper_trail/tree/master

Avatar

This looks good.

You may also like my PaperTrail gem, which doesn't require you to touch existing records to store their starting state. It also lets you recover destroyed records, and copes with intermediate schema changes. It also records who was responsible.

Avatar

I think it filter both because it match the start of the strings, thought I haven't tested myself.

I mean if there is filter for password and you have password_field1, password_field2, password_field3, it will [FILTER] those 3.

Avatar

If anyone wants to do a "diff" output with this to compare versions, check out HTMLDIff
http://github.com/myobie/htmldiff/tree/master

It will put new stuff in green, old in red with strikethrough, etc.

I setup something like this with the acts_as_audited plugin a while back which is also good, looks very similar.

Avatar

I'm wondering how to get this to work with associated models, so if the associations change this is reflected in the versioned model.

I could implement a simple after_update method that touches the parent (i.e. versioned) model when the associated model is updated but I'm wondering will the parent model recognise the changes. Particularly as the main aspect that will change on the associated model is the attached file via paperclip!

Avatar

@Aditya, fixed, thanks!

@August, the wiki_link helper method is inside the application_helper.rb file. Check out the full source code link in the show notes.

@Shane, thanks for pointing this out! I hadn't heard of has_versioning.

Avatar

Might I also suggest taking a look at has_versioning:

http://github.com/kkurach/has_versioning/tree/master

It doesn't only version models, it will also version associations (though not yet via has_many through) and allows for rollback.

You can even lump a whole bunch of changes into a single changelist so that you can later undo them all at once later if need be...

Avatar

Out of curiosity, where's the wiki_link helper in your show.html.erb form?

Avatar

Hi Ryan,
Minor typo in your blog post, it's vestal_versions i believe, not vistal_versions.

Cheers,
Aditya

Avatar

this is much nicer than acts_as_versioned! Thanks for pointing this out.

Avatar

Hi Ryan!
Great Screencast. I use Searchlogic in my hobby project. But currently I have a little problem with Searchlogic.

For example:
 1: User.login_or_email_like("foobar")
 2: User.search(:login_or_email_like => "foobar")

Correct me if I am wrong but these two should have the same result.
The former one works fine, but the latter one does not it returns all Users.

Currently I read something about cells (http://cells.rubyforge.org/b) in RailsWay. Did you know cells?

Hear you tomorrow

Avatar

Would it be possible to get use flash[:notice] to display the loading message?

Avatar

Hello Ryan (and guys),
instead having
/#page=2
/#page=3
I'm trying to have
/#projects
/#tasks

Basically I'm trying to filter my elements by keyword, but I have hard time to achieve so

Is there a way to create such a URL?

Thanks

Avatar

I was using a custom action instead of index.

To make Rails execute the code in custom_action.js.erb (as opposed to index.js.erb), I had to use a respond_to block with format.js, like this:

respond_to do |format|
      format.html
      format.js
end

I'm not sure why Rails doesn't respond to js requests by default?