Great Episode! I am a noob to rails and your railscasts have got me excited for ruby on rails. I am just starting out, but have watched almost half of your videos in the past week. I am starting a few new projects and going to dive right in trying to use it.
I have been reading about keeping things DRY in Rails and just wondering about the two helper methods. sort_column and sort_direction. With multiple models, I would have to specify those functions in every model that needs to be sorted. Is this the way to do it, or would you extract those functions out into more global help methods and then call them some other way.
If that is out of scope for the video i understand. i am just trying to wrap my head around best practices.
the plugin had change, i guess the author was improve it to fit the Rails3 , I've seen the solving by pasting the old codes , watch http://stackoverflow.com/questions/3356955/safe-concat-error-while-using-table-builder-rails-plugin
@Omar: But what if you have so many results that you have to use a pager? You don't have all the data in the browser to re-sort, so you have to ask the server to do it.
Great screencast, I just implemented the same thing, however I used CSS triangles to show the sort order in combination with the :after pseudo element. Unfortunately :after doesn't work in IE6/7 natively, but it saves a HTTP request for the modern browsers.
@Tom, maybe just get the @products and then ruby sort them anyway you like. It worked for me on a small project -- I didn't need to check speed or mem perf for that. You should though. If you chose this route, you should probably put that function into the model. Need more?
@Tom: If your sort can't be implemented at the database level, then you should probably first examine 2 things:
1. Your database schema. It should support your most common use cases well. Do you need a cache column for your sortable data (say, comments_count, or something) or is there something else?
2. Is it absolutely necessary to sort on the column you're trying to add? If you're unable to do the sort in the database, you're going to have to load up the entire set of all records to do it in Ruby code, even if you're paginating (unless you only want your sort column to sort the current page of results).
Assuming you're still sure you want to do it, then you would most likely want to create another method that you call in your index action, which will use Ruby's Enumerable#sort to do the trick.
Another great episode. Please don't apologize for doing a "basic" show. It's refreshing!
I did a similar implementation, but used HTML entities for the arrows ▲ and ▼.
Also I didn't make private functions in the controller but instead used "unless yadda-yadda" in the index method. I can see how this might be DRYer here because you use those functions in the helper too (I didn't).
For use with Rails 3, I would like to recommend Ernie Miller's meta_search gem. Its amazingly simple and yet so sophisticated. I really prefer it more than searchlogic lately.
@Jim, that was kind of a bad example, what if it was something that could not be replicated in SQL? If it was a method that did alot more than just a concat? How would you implement this then?
@Tom: you can implement what you want by modifying the 'sort_column' method to return an equivalent sql method that will be used by your database to order the fetched records.
Wow, it's freaky how you seem to always release casts right when I'm trying to figure something out. Just sat down yesterday and added sortable columns to my wish list.
Very interesting episode, I just wondering how good is to write code in one line which do so much things (and where is the limit!)... it is consider a good practice?
Hi Ryan,
Is there a way to display different colours for text in tables.
EG. I have a table with some fields and due date field.
I want to display the text in red if due_date > Date.today and in green colour if due_date < Date.today
Awesome. But, how do I customize the register of a user? I mean, not only email and password, I'd want to provide state and city, for example. So I put some code in the registrations/new.html.erb and it's fine, however also it's nice to edit, so I put some code in the registrations/edit.html.erb, and it did not work.
http://pastebin.com/VBmCc7NG
error: undefined method `avatar' for :user:Symbol
If you determine adding a global cache is worth it (with @@all) then it's best to call "freeze" on each object in the cache. This way you ensure the state of the object doesn't change.
Can i use whenever to pull a list of all crontab entries on multiple server to a file/database???? i just need to maintain a list of all the crontab entries on my linux/unix servers.
In your video you state "Do NOT use sudo when using RVM." Yet, it is absolutely the ONLY way I can even get this to work. Otherwise I get a permission's error. Why would you not use sudo with RVM?
I get a:
/usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- rails/generators (MissingSourceFile)
When i try to run the generator. (rails 2.3.8)
I wanted to update my previous comment. I've had an opinion reversal on Devise. After using Devise on another project, I am starting to see the light.
Just add your own User routes, controller and views, then add whatever you need to the generated model. Everything seems to work great so far for me.
Also, in case you are having problems getting gmail to work... I mentioned this in another comment (different episode): use your whole email address for user_name in your action mailer config, e.g. :user_name => 'user@gmail.com' not just :user_name => 'user'. I hope that saves someone some time.
Apologies for the previous post. It turned out that I was using the wrong rails.js with the wrong version of jquery (talk about double-trouble!).
Thanks.
Bharat
Hello Ryan,
I am using jQuery like you and I followed the instructions for using it. My delete/destroy link refuses to work. This worked fine in Rails 2.3.8. Is there a way to debug this problem?
Thanks for the wonderful work that you do. Makes all our lives that much better.
Regards,
Bharat
# Randomly insert the correct answer and response into the shuffled array.
random_insert = rand(4)
answer_list.insert(random_insert,["#{correct_ans_1} *", self.correct_anno, self.question_pict])
# Create hash to parse into virtual attributes.
formatted = {
:complete_question => "#{question} \r\r\r A. #{answer_list[0][0]}\r B. #{answer_list[1][0]}\r C. #{answer_list[2][0]}\r D. #{answer_list[3][0]}\r E. #{answer_list[4][0]}\r",
:anno_1 => answer_list[0][1],
:anno_2 => answer_list[1][1],
:anno_3 => answer_list[2][1],
:anno_4 => answer_list[3][1],
:anno_5 => answer_list[4][1],
:anno_pict_1 => answer_list[0][2],
:anno_pict_2 => answer_list[1][2],
:anno_pict_3 =>
end
Hi!... I'm using ubuntu. After trying this casts. When i run the server, this is the output:
rails s
=> Booting WEBrick
=> Rails 3.0.0.rc application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
/home/israel/Desktop/projects/mailit/config/initializers/setup_mail.rb:12: uninitialized constant DevelopmentMailInterceptor (NameError)
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.rc/lib/rails/engine.rb:201
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.rc/lib/rails/engine.rb:200:in `each'
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.rc/lib/rails/engine.rb:200
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.rc/lib/rails/initializable.rb:25:in `instance_exec'
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.rc/lib/rails/initializable.rb:25:in `run'
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.rc/lib/rails/initializable.rb:50:in `run_initializers'
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.rc/lib/rails/initializable.rb:49:in `each'
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.rc/lib/rails/initializable.rb:49:in `run_initializers'
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.rc/lib/rails/application.rb:134:in `initialize!'
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.rc/lib/rails/application.rb:77:in `send'
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.rc/lib/rails/application.rb:77:in `method_missing'
from /home/israel/Desktop/projects/mailit/config/environment.rb:5
from /home/israel/Desktop/projects/mailit/config.ru:5:in `require'
from /home/israel/Desktop/projects/mailit/config.ru:5
from /usr/lib/ruby/gems/1.8/gems/rack-1.2.1/lib/rack/builder.rb:46:in `instance_eval'
from /usr/lib/ruby/gems/1.8/gems/rack-1.2.1/lib/rack/builder.rb:46:in `initialize'
from /home/israel/Desktop/projects/mailit/config.ru:1:in `new'
from /home/israel/Desktop/projects/mailit/config.ru:1
Thanks, Ryan! For anyone who needs more, I put together a Rails 3 example app (with a detailed walk-through tutorial) that shows how to set up subdomains in Rails 3 using Devise for authentication: http://github.com/fortuity/rails3-subdomain-devise (click above for the link). For anyone who needs a quick start for blog-style subdomains using Rails 3.
:domain => :all doesn't work in the Rails 3 release candidate (it results in an InvalidAuthenticityToken error). The issue is resolved in Rails 3 master on GitHub with commit fd78bb72704554737117 by Bryce Thornton on 14 Aug 2010.
Great Episode! I am a noob to rails and your railscasts have got me excited for ruby on rails. I am just starting out, but have watched almost half of your videos in the past week. I am starting a few new projects and going to dive right in trying to use it.
I have been reading about keeping things DRY in Rails and just wondering about the two helper methods. sort_column and sort_direction. With multiple models, I would have to specify those functions in every model that needs to be sorted. Is this the way to do it, or would you extract those functions out into more global help methods and then call them some other way.
If that is out of scope for the video i understand. i am just trying to wrap my head around best practices.
thanks again!
@yauco
the plugin had change, i guess the author was improve it to fit the Rails3 , I've seen the solving by pasting the old codes , watch http://stackoverflow.com/questions/3356955/safe-concat-error-while-using-table-builder-rails-plugin
@Omar: But what if you have so many results that you have to use a pager? You don't have all the data in the browser to re-sort, so you have to ask the server to do it.
I've seen pure Javascript code that does the same rather then using HTTP GET.
I wonder if there is a good Ruby to XSLT translator instead of working hard in XSLT at the first place ...
Anyway, thanks for another great video cast on Rails, as always I learn a lot from your casts.
Great screencast, I just implemented the same thing, however I used CSS triangles to show the sort order in combination with the :after pseudo element. Unfortunately :after doesn't work in IE6/7 natively, but it saves a HTTP request for the modern browsers.
@Tom, maybe just get the @products and then ruby sort them anyway you like. It worked for me on a small project -- I didn't need to check speed or mem perf for that. You should though. If you chose this route, you should probably put that function into the model. Need more?
@CodeOfficer: thanks for the plug!
@Tom: If your sort can't be implemented at the database level, then you should probably first examine 2 things:
1. Your database schema. It should support your most common use cases well. Do you need a cache column for your sortable data (say, comments_count, or something) or is there something else?
2. Is it absolutely necessary to sort on the column you're trying to add? If you're unable to do the sort in the database, you're going to have to load up the entire set of all records to do it in Ruby code, even if you're paginating (unless you only want your sort column to sort the current page of results).
Assuming you're still sure you want to do it, then you would most likely want to create another method that you call in your index action, which will use Ruby's Enumerable#sort to do the trick.
Another great episode. Please don't apologize for doing a "basic" show. It's refreshing!
I did a similar implementation, but used HTML entities for the arrows ▲ and ▼.
Also I didn't make private functions in the controller but instead used "unless yadda-yadda" in the index method. I can see how this might be DRYer here because you use those functions in the helper too (I didn't).
For use with Rails 3, I would like to recommend Ernie Miller's meta_search gem. Its amazingly simple and yet so sophisticated. I really prefer it more than searchlogic lately.
http://github.com/ernie/meta_search
@Jim, that was kind of a bad example, what if it was something that could not be replicated in SQL? If it was a method that did alot more than just a concat? How would you implement this then?
~ Tom
Sorry I got the problem,
Since owner .gem folder is root.
I changed it from root to normal user using chown, and it is working fine.
In the private methods in the controller I would have used a regexp like so:
params[:sort][/(asc|desc)/] || "asc"
Is it a bad idea?
Wouldn't you need to escape the params in the order clause to prevent sql injection, or is Rails 3 doing this automatically now?
I think it's much better to let JS do it, this would be better for the server, and a way more faster for the user.
@Tom: you can implement what you want by modifying the 'sort_column' method to return an equivalent sql method that will be used by your database to order the fetched records.
I am getting following permission error
ERROR: While executing gem ... (Errno::EACCES)
Permission denied - /home/rahul/.gem/specs
while running an command(Installing rails3)
gem install tzinfo builder memcache-client rack rack-test rack-mount erubis mail text-format thor bundler i18n
Some of the my observations are
rahul@LinuxGuru:~$ rvm list
rvm rubies
=> ruby-1.9.1-p378 [ x86_64 ]
rahul@LinuxGuru:~$ ruby -v
ruby 1.9.1p378 (2010-01-10 revision 26273) [x86_64-linux]
rahul@LinuxGuru:~$ which ruby
/home/rahul/.rvm/rubies/ruby-1.9.1-p378/bin/ruby
rahul@LinuxGuru:~$ which gem
/home/rahul/.rvm/rubies/ruby-1.9.1-p378/bin/gem
Awesome, however, what if the column you want to search on is a computed column?
Say, its product.release_code
where:
def release_code
"#{name.slice(0,2).upcase}-#{released_at..strftime("%Y-%m-%dT%H:%M:%S")}"
end
How would you go about sorting on this column on the table as its not a column in the database?
Would you first check to see if the column exists on the table and if not do an array sort?
Thanks,
~ Tom
Wow, it's freaky how you seem to always release casts right when I'm trying to figure something out. Just sat down yesterday and added sortable columns to my wish list.
Amazing.
Thanks for all your hard work.
This is one of your best episodes!
Thank you very much.
Even if it is for beginners in a way, it is nice to see your logic and way of doing things.
Great cast, as usual! For those folks looking for a plugin that does this and more for Rails 3, I'd like to add a suggestion to try out MetaSearch.
Alternately, if you want to maintain search (or other GET params), be sure to include them in your call to link_to in the sortable method.
Very interesting episode, I just wondering how good is to write code in one line which do so much things (and where is the limit!)... it is consider a good practice?
Hope to see more railscasts like that! :)
Hi Ryan,
Is there a way to display different colours for text in tables.
EG. I have a table with some fields and due date field.
I want to display the text in red if due_date > Date.today and in green colour if due_date < Date.today
Thanks,
Aj
@Israel B., try posting your issue on railsforum.com along with your source code of setup_mail.rb
Awesome. But, how do I customize the register of a user? I mean, not only email and password, I'd want to provide state and city, for example. So I put some code in the registrations/new.html.erb and it's fine, however also it's nice to edit, so I put some code in the registrations/edit.html.erb, and it did not work.
http://pastebin.com/VBmCc7NG
error: undefined method `avatar' for :user:Symbol
Any help?
If you determine adding a global cache is worth it (with @@all) then it's best to call "freeze" on each object in the cache. This way you ensure the state of the object doesn't change.
Hi Guys
Can i use whenever to pull a list of all crontab entries on multiple server to a file/database???? i just need to maintain a list of all the crontab entries on my linux/unix servers.
why I couldn't nest an add field in another without having a double javascript escaping, resulting to a messy display in the view.
Now I am planing to leave for sleeping. I hope you will add that. Thanks
is a excellente tutorial , i have one question , if my form is remote, I can do the same? or that would be the method
In your video you state "Do NOT use sudo when using RVM." Yet, it is absolutely the ONLY way I can even get this to work. Otherwise I get a permission's error. Why would you not use sudo with RVM?
Great stuff!
How can I set a maximum width/height to be cropped?
Thanks MattR for sharing that. And thanks Ryan for this great screencast.
Installing from http://github.com/kozy/daemon_generator.git fixes the bug.
I get a:
/usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- rails/generators (MissingSourceFile)
When i try to run the generator. (rails 2.3.8)
When an object has errors, it will redirect to the location if specified. It is not handled in this screencast, but it is not working as it should.
http://gist.github.com/535722
I wanted to update my previous comment. I've had an opinion reversal on Devise. After using Devise on another project, I am starting to see the light.
Just add your own User routes, controller and views, then add whatever you need to the generated model. Everything seems to work great so far for me.
Also, in case you are having problems getting gmail to work... I mentioned this in another comment (different episode): use your whole email address for user_name in your action mailer config, e.g. :user_name => 'user@gmail.com' not just :user_name => 'user'. I hope that saves someone some time.
Apologies for the previous post. It turned out that I was using the wrong rails.js with the wrong version of jquery (talk about double-trouble!).
Thanks.
Bharat
Hello Ryan,
I am using jQuery like you and I followed the instructions for using it. My delete/destroy link refuses to work. This worked fine in Rails 2.3.8. Is there a way to debug this problem?
Thanks for the wonderful work that you do. Makes all our lives that much better.
Regards,
Bharat
Is there a way to use each of the values in 'formatted" as individual virtual attributes?
Method in class "Question":
def format_for_presentation
# Reformat question attributes for presentation.
# Create array and shuffle incorrects.
answer_list = [
[incorrect_ans_1, incorrect_anno_1, incorrect_ans_pict_1],
[incorrect_ans_2, incorrect_anno_2, incorrect_ans_pict_2],
[incorrect_ans_3, incorrect_anno_3, incorrect_ans_pict_3],
[incorrect_ans_4, incorrect_anno_4, incorrect_ans_pict_4],
[incorrect_ans_5, incorrect_anno_5, incorrect_ans_pict_5],
[incorrect_ans_6, incorrect_anno_6, incorrect_ans_pict_6]
].shuffle
# Randomly insert the correct answer and response into the shuffled array.
random_insert = rand(4)
answer_list.insert(random_insert,["#{correct_ans_1} *", self.correct_anno, self.question_pict])
# Create hash to parse into virtual attributes.
formatted = {
:complete_question => "#{question} \r\r\r A. #{answer_list[0][0]}\r B. #{answer_list[1][0]}\r C. #{answer_list[2][0]}\r D. #{answer_list[3][0]}\r E. #{answer_list[4][0]}\r",
:anno_1 => answer_list[0][1],
:anno_2 => answer_list[1][1],
:anno_3 => answer_list[2][1],
:anno_4 => answer_list[3][1],
:anno_5 => answer_list[4][1],
:anno_pict_1 => answer_list[0][2],
:anno_pict_2 => answer_list[1][2],
:anno_pict_3 =>
end
What would the code look like to read the codes.csv file in db/seedData directory? It has three columns. name, date, location
Thank you!!
Hi!... I'm using ubuntu. After trying this casts. When i run the server, this is the output:
rails s
=> Booting WEBrick
=> Rails 3.0.0.rc application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
/home/israel/Desktop/projects/mailit/config/initializers/setup_mail.rb:12: uninitialized constant DevelopmentMailInterceptor (NameError)
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.rc/lib/rails/engine.rb:201
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.rc/lib/rails/engine.rb:200:in `each'
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.rc/lib/rails/engine.rb:200
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.rc/lib/rails/initializable.rb:25:in `instance_exec'
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.rc/lib/rails/initializable.rb:25:in `run'
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.rc/lib/rails/initializable.rb:50:in `run_initializers'
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.rc/lib/rails/initializable.rb:49:in `each'
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.rc/lib/rails/initializable.rb:49:in `run_initializers'
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.rc/lib/rails/application.rb:134:in `initialize!'
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.rc/lib/rails/application.rb:77:in `send'
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.rc/lib/rails/application.rb:77:in `method_missing'
from /home/israel/Desktop/projects/mailit/config/environment.rb:5
from /home/israel/Desktop/projects/mailit/config.ru:5:in `require'
from /home/israel/Desktop/projects/mailit/config.ru:5
from /usr/lib/ruby/gems/1.8/gems/rack-1.2.1/lib/rack/builder.rb:46:in `instance_eval'
from /usr/lib/ruby/gems/1.8/gems/rack-1.2.1/lib/rack/builder.rb:46:in `initialize'
from /home/israel/Desktop/projects/mailit/config.ru:1:in `new'
from /home/israel/Desktop/projects/mailit/config.ru:1
Im new in Rails.
Dont know how to solve this...
In Rails 3 application path is Rails.root instead of RAILS_ROOT.
What is the best way to set up a rails environment that has two cache stores? One is for MemcacheD and the other MemcacheDB.
Hi, i'd like to know.. is it possible to use the new rails router in non rails apps?
And if how?
thx :)
Thanks, Ryan! For anyone who needs more, I put together a Rails 3 example app (with a detailed walk-through tutorial) that shows how to set up subdomains in Rails 3 using Devise for authentication: http://github.com/fortuity/rails3-subdomain-devise (click above for the link). For anyone who needs a quick start for blog-style subdomains using Rails 3.
@Doug Mayer (#36):
@thb (#47):
:domain => :all doesn't work in the Rails 3 release candidate (it results in an InvalidAuthenticityToken error). The issue is resolved in Rails 3 master on GitHub with commit fd78bb72704554737117 by Bryce Thornton on 14 Aug 2010.
. I hope I can improve through learning this respect. Bu
Now the *big* question, Ryan, is railscasts.com **actually** running in Rails 3 now? :-)
comments are very good friends thank you.
@Andy That's exactly the problem i'm facing right now.. anyone figure out a way to fix this issue?