RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

Hi Ryan, would it not be better to use a before_save callback to call assign_tags, and then use find_or_initialize_by_name instead of find_or_create_by_name. That way, the tags all get saved in one atomic action.

Avatar

if you're getting this error:

./scrapi.rb:5: uninitialized constant Scraper (NameError)
from /opt/ruby-1.8.7-p72/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'

then add this 'gem 'scrapi'

require 'rubygems'
gem 'scrapi'
require 'scrapi'

-mark

Avatar

Hello Ryan,

First, thanks for all the webcasts.

I am trying to implement paypal and I followed your previous webscast and it is working well.
Note that I did not do a link to paypal in the form but I integrated a
redirect_to(@order.paypal(r_url,not_url))
in my controller.
I was wondering if I needed to encrypt as the url was built in my controller?

Thanks

Avatar

I'm struggling with multiple radio box groups. I don't want (ideally) to solve this problem with javascript if I can help it. Has anyone figured a way to get multiple radio button groups working without JS?

Thanks!

Avatar

What's the best way to cache the individual records in a render like this:

render :xml => @products.to_xml

I'm caching the entire render with action caching, but when I expire the action, I shouldn't have to re-render each individual product's XML, possibly including deep associations. Is there a way without throwing it all into the views and fragment caching there?

Avatar

I'm having some problems with my Worker on my production machine and I can't see any of the error logs.

I've set my env config.log_level to :debug and I still can't find any logs from workling.

I've been trying to figure this out for a week now. I hope someone can help.

Avatar

I want this locally on my machine.
Product.name_not_like("Video").price_gt(5).price_lt(200)

Produces

SELECT * FROM `products` WHERE (products.price < 200)

Avatar

Great screencast Ryan. Thank you.

I am unable to access my new vHosts ServerNames thru IE in Vista running in Parallels. Any insight on this or references would be greatly appreciated.

Thanks again

Avatar

Hi Ryan, thanks for all your screencasts.

I was wondering about the bit where you edited the content of the textarea from Safari in Textmate. Do you have a plugin for Safari where you can edit in Textmate?

Avatar

I came up with a pretty good workaround for the HABTM problem.

If you nest the relationships and then inside the lower level, at the end add an ActiveRecord::Base.execute command like this http://gist.github.com/176652 , it creates a single record for the join table that makes habtm work well.

Avatar

Thanks, Ryan! Great episode.

I was surprised that there was no mention SearchLogic's interoperability with will_paginate. Paginating search seems to be one of the big hurdles newbie Rails developers tackle.

All one needs to do is call the paginate method on the search, thus:

@search = Product.search(params[:search])
@products = @search.paginate( :page => params[:page] )

Avatar

@jDeppen: go ahead and use SearchLogic for the front-end search, but make sure the controller scrubs out any values in the search hash that you don't want there.

http://gist.github.com/176481

This way, you can even define defaults for the search.

Avatar

I got the following error, when i followed your code, please assist me..
Thanks

 NameError in OrdersController#express

undefined local variable or method `products_url' for #<OrdersController:0xb6933454>

RAILS_ROOT: /home/shanmuga/shapero/shapero
Application Trace | Framework Trace | Full Trace

app/controllers/orders_controller.rb:6:in `express'

Request

Parameters:

None

Avatar

One thing I noticed when using searchlogic is the class of the return value is not what you expect: -

1. User.find_by_login('bob').class -> returns User

2. User.login_equals('bob').class -> returns ActiveRecord::NamedScope::Scope

This is an important distinction as you cannot call methods such as .destroy on the second example.

Avatar

@Robert Matei

You can apply templates to existing apps using rake:

rake rails:template LOCATION=/path/to/template.rb

Avatar

Is there any way to get the logger to run automatically when script/console is started up? I can't imagine not wanting it active. Cheers.

Avatar

About a year ago I wrote a plugin to provide touch functionality to models. I've been using it to fuel another plugin of mine I call conflict warnings(http://github.com/EmFi/conflict_warnings). To deal with the the race condition that arises when two users modify the same resource asynchronously.

It relies heavily on touch because the pages I have a user acting on, are usually a collection of child records. Caching is a big help in this respect.

Avatar

Excellent railscast. Thanks for doing these. I was up and running with Linkpoint/FirstData in no time.

I had the same question as pjammer. If the transaction fails I guess we need to implement the update action for the order controller. My only concern with this is that it might inhibit debugging of transaction problems since the order database will change as the user makes corrections.. but then again most of the information they are sending that would cause an error is private so probably not an issue.

Thanks again.

My linkpoint setup notes are here
http://tonycode.com/wiki/index.php?title=First_Data_Ruby_on_Rails_GEM

Avatar

I've created a custom formatter that generates better HTML output for use with your cucumber projects and textmate

http://github.com/raldred/cucumber_textmate/tree/master

Avatar

I've created a custom formatter that generates better HTML output for use with your cucumber projects and textmate

Avatar

Thanks for this awesome Railscast Ryan! One small question remains: I'd like to set the fragment to a certain string like '#/action/subaction'. Any ideas how to manage this without having some key-value pair in my url?
Thanks and greetings,
Joe

Avatar

In my setup, god.rb is reporting that my worklings are going over the memory limit (200MB) about every day or so, and it restarts them. I can't seem to figure out why the memory is blowing up from 70MB => 230MB every once in a while... does anyone have any suggestions on how to go about investigating?

Avatar

I'm attempting to clean up orphaned tags by using a after_destroy hook, but it doesn't seem to even be fired?

Does the virtual writer not fire the destroy events?

Thanks!

Avatar

I fixed all bugs and working code you can download from
www.rubyf.info/files/polimorphic_work0.zip

Avatar

Is it possible to set one of the columns in the table to use a different type of font than the rest?

Avatar

Hi Ryan Nice artical.It worked for me when i take
@attendence= Attendence.find(:all)
but when i take
@attendence= Attendence.find(:all,:conditions => ["date >= ? AND date <= ? AND grade=?",params[:start_date],params[:end_date],params[:grade]])
not displaying values in pdf file.In show.html.erb the values are displaying correctly.please tell me how can i take:Here is my code:

controller:

def show
 @attendence= Attendence.find(:all,:conditions => ["date >= ? AND date <= ? AND grade=?",params[:start_date],params[:end_date],params[:grade]])
end

show.pdf.prawn
pdf.text " Attendence Report", :size => 30, :style => :bold

pdf.move_down(30)
@attendence.each do |attendence|
  
    pdf.text attendence.date
   
end

Avatar

Nice, but the better way is to use ThinkingSphinx with sphinx_scope.

Avatar

Hi
ruby script/plugin install git://github.com/thorny-sun/prawnto.gi not working for me iam using rails 2.3.2 on windows xp.any solution?help me

Avatar

Packed full of win! The underscore in IRB hint alone was well worth the download, but being able to get rid of several lines of complex code using searchlogic.... excellent episode. Keep it up.

Avatar

Great screencast. Worth it just for the tip about irb and assigning variable with an underscore!

Avatar

Actually it seems that they are identical now. Silly me.

Avatar

While this searchlogic is great, it's pretty outdated. You should not use 'searchlogic' gem. Instead grab the 'binarylogic-searchlogic'. See: http://www.binarylogic.com/2009/06/15/searchlogic-v2-officially-released/

Basically it's a complete rewrite that deals with searches quite differently and way cleaner.

Avatar

@chris make an .irbrc file in your home folder and put this in the file

http://gist.github.com/174133

enjoy.

Avatar

I'm trying to use Searchlogic and allow users to save searches for my real estate website. I'm trying to make it a ListingSearch resource.

It seemed like Ryan was suggesting using Searchlogic for only admin purposes. Should I roll my own "old school" search?

Anyone have any thoughts?

Avatar

@BasicObject I ran into the same problem with SearchLogic and decided to extend my own ar_helper plugin to do just that, take a look at the #search method here at: http://github.com/vanntastic/ar_helper/tree/master. In your example above, you can do this instead:

<script src="http://gist.github.com/174016.js"></script>

Avatar

How do I implement searching multiple columns? Say I have an address book database and I want one search field to query the name column, the address column and the email column. How might this be accomplished using searchlogic and a single search field?

Avatar

@vinnyt For hirb documentation see http://tagaholic.me/hirb/doc and http://github.com/cldwalker/hirb/tree/master#readme .

Avatar

Nice! Still playing around with it...getting inconsistent results. Maybe my rails version(2.2.2)?

Also, how does one "preload" commands like the ActiveRecord::Base::logger = Logger.new(STDOUT) or a require "<blah>" so they are available as soon as you start the console?

Avatar

Hi Ryan,

I have a question, in the collection_select if users select blank, will it means select :all or select blank? I hope it's the former one, I'll try it out later.

Avatar

Ryan,
great episode, as always. In any event I am stuck on the hirb stuff, I tried rtfm on the hirb page but it doesn't work can you give me a quick pointer on this one?

Avatar

Ahem, I mean searchlogic in the comment above.

Avatar

@Roy van der Meij

I don't think you have to put a route. @search is an array of products stored in the index action of Products controller, so it should do the trick with form_for.

Avatar

Hi Ryan,

Thanks for this screencast -- this would have saved me a lot of time last week! ;) I feel like the safety/security issues of using this method for a public search feature could be averted if before using the search method, you slice the params hash to only include the parameters that were expected from the form. Then any other parameters wouldn't be acted upon. What do you think of that?

Also, do named scopes (as well as authlogic) automatically escape values, removing the need to call h() manually?

Cheers,
Alex

Avatar

Everything works perfect for me except one thing. Is it possible to include the page number in the pdf as "Page n of n" I can get the current page but I haven't been able to find a way to get the total number of pages.

Avatar

The code is written in AttendenceReport index method.
when in run
http://localhost:3000/attendence_report/att.pdf
then it is giving error:
No action responded to att. Actions: index
and index working properly.help me

Avatar

Hi Ryan
  Iam using rails 2.3.2.Whrn i run http://localhost:3000/att.pdf
It is giving following error:
No route matches "/att.pdf" with {:method=>:get}
 In routes.rb i have
 map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'
  map.connect ':controller/:action.:format'
please help me why iam getting this error

Avatar

Great job Ryan, this is a great video. You did an excellent job. It definitely beats my boring tutorials.

Keep up the good work.

Avatar

Great episode! But I have one question.

How come form_for @search get's mapped to the index action of ProductsController, where is that route configured?

Avatar

Another great cast Ryan. Thanks.

One small bug I found. At 13:00 you call it Authlogic, instead of Searchlogic. Its probably too late to fix that but I thought I'd mention it :)

Avatar

Ryan,
Thanks for all your screencasts! They are lifesavers!

I am really having trouble with the updating of attributes. The build of a new project (routinglist) works great...but I get the error "'can't convert String to Integer" in the Existing_Detail_Attributes= method. the creation of a new RoutingList with Details works fine, it is only when I try to update the RoutingList and save the attributes. If I change detail.id.to_s to just detail.id, I get no errors, but it deletes all my details.
Any ideas? BTW I am running this on Rails 2.0.2 (I upgraded to 2.3.3 but I never could get Mongrel to start, so I am still using 2.0.2)

Here are the 2 relevant methods in the model (detail.id=13):

def new_detail_attributes=(detail_attributes)
   detail_attributes.each do |attributes|
       routlist_details.build(attributes)
   end
 end

 def existing_detail_attributes=(detail_attributes)
   routlist_details.reject(&:new_record?).each do |detail|
     logger.info 'detail='+detail.to_yaml
     attributes = detail_attributes[detail.id.to_s]
     if attributes
         detail.attributes = attributes
     else
         routlist_details.delete(detail)
     end
   end
 end

Thanks - Frank