RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

May have said it already, but you asked for it, so here it is. This episode was great and I would love to see more internals videos like this. Not just routing but maybe ActiveRecord chainable queries and the like as well.

Thanks for the great work!

Avatar

Mind. Blown. Excellent episode, very much looking forward to more like this in the future.

Avatar

If you use Rails 3, then you should remove "h(" in application_helper.rb :
link_to_function(name, h("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")"))

to

link_to_function(name, "add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")")

Thanks to Ruslan Voloshin from rubyclub.com.ua

Avatar

How can we migrate comment text along with individual columns.

For column "status"
Comments are :
0- inactive
1- active
2 - hold

Need this data to fall when run rake db:migrate .

Avatar

Many thanks Ryan - a gem of a gem.

One thing:

I'm having problems getting cucumber to recognise cancan authorisations. Capybara says the correct user (with correct roles) is logged in, but displays the cancan "not authorized" message. Accessing the same page outside of cucumber, with the same account, offers access.

Any ideas what I'm missing? I've tried everything I can think of and I'm pretty stumped. If you have any suggestions I'd be really appreciative. Relevant code here: http://dpaste.com/242546/

Avatar

For those of you having validation errors due to the child being saved before the parent, this has been fixed in Rails 2.3.6 with the :inverse_of option.

See these links:
https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/2815-nested-models-build-should-directly-assign-the-parent

and
http://github.com/oggy/inverse_of

Avatar

Hello. Why dont work "Add answer" and "Add squestion" in Rails 3 Ruby 1.9.2 ?

Avatar

I forked table_builder to create a Rails 3 compatible gem version:

http://github.com/jchunky/table_builder

Avatar

Thanks, Ryan! I posted my experience using your guide to port a Rails 2.3.8 application to Rails 3.0.0 with Ruby 1.8.7 here:

http://www.mightyjupiter.com/2010/09/upgrading-to-rails-3.html

Avatar

The bigger movie (the one that is 43MB), when at 5:02, when the program runs using TextMate, the result is not shown when the movie is played inside the browser using the current QuickTime 10.0. The small video does show the result.

If VLC is used, then both the large video and small video show the result. Maybe it is some format issue, or can the large video run through Handbrake and keep the same size so that it is good for both QuickTime and iPod and iTV?

Also, the background of the text being greyish a little, doesn't bring the contrast of the text relative to the background. If a black background can be used, it will be best.

Avatar

As always, nice screencast.

I personally wouldn't use these gems though. Sure, keeping things DRY is generally a good thing, but there are times when being explicit and transparent about exactly what is happening is worth the price of a small amount of repetition.

So, I'll gladly keep going with the standard RESTful controller, with its 7 skinny actions.

Avatar

Another great episode Ryan.....I have started to re-write an opensource crm program that was done in php/mysql and I have since made the leap to re-write the whole CRM program from the ground up using Ruby/Rails.
I have all the episodes on my ipod so when I get stuck or need help remembering some finer points, I can quickly find then.
From one Opensource and Free provider to another I'll be making a donation now..
Keep up the great work Ryan..

Avatar

Thanks for this episode. I was looking around for a nice replacement for make_resourceful and this is great! :)

Avatar

+1 to what @iain said

The use of proc is not recommended. Instead, you should use lambda or Proc.new. lambda is recommended in most cases, IMHO.

I also agree with what @iain said about using #scope with a Proc. #scope is great but, if you need to pass variables, a class method is *much* cleaner.

Great screencast. Glad to see InheritedResources working with Rails3!

Avatar

Ryan thanks for shedding some rails light to my noob darkness. I have a problem with :category_id. I get the "undefined method" error. I read the collection_select api for rails 3 and it doesn't seem I'm doint anything wrong. Yet I still get the error message. I'm using this:

<%= f.collection_select :category_id, Category.find(:all), :id, :name, :prompt => true%>
I just want to add a category to each product but I get this annoying error.

PS: Category and Product are both generated with scaffold.

Thank you again for your time and the great Rails screencasts. Kudos man.

Avatar

ryan, please put a f*ckin captcha to the comments form.

Avatar

Thanks Ryan for this. I was just working om something like this for one of my projects.
Have you seen Robert Fischer jQuery port of PeriodicalUpdater ( http://github.com/RobertFischer/JQuery-PeriodicalUpdater ) That can do what you build yourself here with just a small change :-)

Please look at it, I found it very usefull.

Avatar

Another cool episode. :)

I'm using inherited_resources for admin part in my application, where I used scaffolds before. Its a great replacement for scaffolding :)

Avatar

Hi Ryan!

Thanks for the great things you're doing! :)

I have a question about this episode.
In the controller code in where condition you compare created_at with Time.at(params[:after].to_i + 1). In rails 2.3.8 datetimes are stored in utc in DB, and Time.at(...) would return a time in the system timezone so inserting Time.at into where condition without explicitly converting it to utc would produce incorrect resulting query. Did they change the way conditions work in rails3?

Avatar

I'm using jquery and when I add I notice the new field isn't going to the correct place so instead of:

$(link).parent().before(content.replace(regexp, new_id));

I use:
$(link).before(content.replace(regexp, new_id));

and this works well.

Avatar

Ok, I know this railscast was just on formatastic, but does anyone have a link/post to how you would show the lisf of problems when viewing the animal?

I can't see a way you can get the list of Problem names when all you have is the Animal ID. (in a single query.)

Basically just need the controller entry:
@animalproblems = ???? params[:id] ??

Avatar

It's much easier to implement subdomains in Rails 3 than in Rails 2.3 (no subdomain_fu plugin required). For anyone looking for a complete example implementation of Rails 3 subdomains with authentication (and a detailed tutorial) here's my repo on Github:
http://github.com/fortuity/rails3-subdomain-devise
and Ryan has a newer Railscast on Subdomains in Rails 3: http://railscasts.com/episodes/221-subdomains-in-rails-3

Avatar

The subject is on the spot as we all tend to forget the little things that can easily speed up our application.

Avatar

well... what about wkhtmltopdf-qt ?
The gem says it has to be installed... and this part is very much more complicated.
Does anyone have experienced it ?

Avatar

Hi,
I am using Devise and want to override a method from a controller also there is a different way for signup.
I am using RAILS = 2.3.5 and DEVISE= 1.0.8
I tried using

 class Mycontroller < Devise::RegistrationsController

    end
and also
 devise_for :users, :controllers => { :registrations => "some_diff_controller" }

But still I am finding that the method is NOT getting overridden. Could you please suggest anything over here

Thanks in advance

Avatar

Thank you Ryan! Thanks helmerj for the Rails 3 fix! :)

Avatar

I have been thinking for a while that this should be implemented in Rails itself. Just makes too much sense.

Avatar

Be careful with the scope with a proc. The proc method behaves differently in Ruby 1.8 and 1.9 and you shouldn't use it (to avoid confusion). It's not a big deal here, but still.

Furthermore, there is no need for "named scopes" like this anymore. IMHO it's much cleaner to just create a class method:

http://pastie.org/1141847

This works _exactly_ the same as defining the scope like you did in the screencast.

One more tip, concerning InheritedResources. If you always want to redirect to the index action after update or create, you can also create a class inbetween ApplicationController and your regular controller. A second -more specific- application controller if you will. I usually have a "Backend::ApplicationController" which acts as the application controller for the admin part of the site. Example: http://pastie.org/1141861

Avatar

@docgecko, I just made the Gemfile be interpreted as Ruby in TextMate.

@Paul, whether or not this takes the abstraction too far depends on your application. I don't recommend it for everything, but if you're building an administration interface with repetitive RESTful controllers I imagine it would clean things up a lot.

@Jorge, thanks! I'll fix the show notes.

@David, thanks for pointing out the dangers of accidentally leaving the destroy action available. If one is concerned about this I recommend always defining the "actions" at the top of the controller even if you are doing all 7.

In the end I only recommend abstracting out the controller actions if the repetition is there in the application. Not all apps work well for this.

Avatar

Love the work you do but this is very dangerous IMO. Its way to easy to have a destroy method when you never intended to have one. Also I've found IR and resource controller to scream N + 1 query. This works for a simple CRUD admin though.

I've just rarely have simple CRUD controllers. =)

I wrote something here about Resource Controller http://www.mailinglistarchive.com/html/spree-user@googlegroups.com/2010-05/msg00211.html

 Thanks for all the work you put in.

Avatar

First I thought, why the hell would I use this? The new rails 3.0 controllers are already so much cleaner when you are using respond_with. But getting rid of all the actions and just adding exceptions where you need them just rocks. This is particularly time saving when dealing with users and only users can see and edit stuff they created, just throw in:
 protected
 def begin_of_association_chain
   current_user
 end
and your already set for all actions. Thank you for another great episode!

Avatar

Hey Ryan nice screencast, one note, you should be using the new interpolation way for internationalization files, instead of {{var}} %{var} should be used.

Avatar

I like this, tho i dont know if id use this. Is it an abstraction too far? a little further and it would start to become active scaffold without the views. i like to see whats happening even at the expense of a little extra code and repetition. each to their own tho. good video +1

Avatar

docgecko, look at http://github.com/drnic/Bundler.tmbundle

Avatar

Brilliant, thanks Ryan!

By-the-way, how did you color your Gemfile?

Avatar

Thank you for your awesome work! You really rock!

Avatar

I am on os x - 10.6.4

I followed this post as closely as possible (even tried a few suggestions in the comments) yet I get the following error:

$ rails topscore
/usr/local/bin/rails: line 9: require: command not found
/usr/local/bin/rails: line 11: version: command not found
/usr/local/bin/rails: line 13: syntax error near unexpected token `('
/usr/local/bin/rails: line 13: `if ARGV.first =~ /^_(.*)_$/ and Gem::Version.correct? $1 then'
$

Any ideas out there?

Avatar

Hey Ryan. Just a tip, instead of doing "<%= escape_javascript(render(@comments)) %>" you can do just (without quotes) <%= (render(@comments)).to_s.to_json %>
i find it a nice trick

Avatar

Hello,

I am just (re)starting with Rails.

If you want to push performance further, you can write the user data serialized to the session if session access/data retrieval is faster than db access/data retrieval. Be aware you have to clean (or update) the user data stored in the session if the users data gets modified.

On a side note: will this still be required with rails3? As far as I understood ARel its only executed "when required" does this mean in this case only once?

Avatar

Trying to follow thru the rspec parts.

Ran the generator (new spec_helper etc) - but rake spec is still giving

no such file to load -- spec/rake/spectask

Not sure where to go with this - am finding some hits on google about the task moving to rspec/core/raketask (http://github.com/rspec/rspec-core/blob/master/Upgrade.markdown) - but I can't see how to apply changes to fix it.

Avatar

I know "push" is a bit of a buzz word at the moment but it's completely justified and you really should give it a try.

Using Pusher - http://pusherapp.com - you get WebSockets (and a Flash fallback) that can be integrated with your Rails app with just a few lines. Check out the quickstart guide http://pusherapp.com/docs/quickstart to see how easy it is. You also get instant feedback - no waiting for 10 seconds - and less load on your server to boot!

Disclaimer: I work at New Bamboo who are the guys behind Pusher.

Avatar

This episode cannot be played on my iPhone 4. Any one has similar experience?

Avatar

The flatten options does break the use of "IN (?)" type searches...

ActiveRecord::PreparedStatementInvalid: wrong number of bind variables (2 for 1) in: <some field> IN (?)

Adding the optional level of 1 to flatten fixes this.

Avatar

Thank you for these awesome tutorials. I learned a ton. This particular example does not work -- cannot add questions or answers in Rails 3.0.0. Please help.

Avatar

I can not get searchlogic to work when using a has_many through model. Is it supposed to? I get method_missing when I try to search for something in the has_many table.

Thanks,
Ryan

Avatar

Well, the post is in reality the freshest on this noteworthy topic. I harmonize with your conclusions and will thirstily look forward to your future updates. Saying thanks will not just be sufficient, for the exceptional lucidity in your writing