@Ekolguy, I would but the by_date method is such an ugly hack that I don't want to show it. Maybe I'll improve it and turn it into an episode some day.
Regarding the routing, The name of the action shows up in the URL so there's no conflict. If I were using restful routes where the article ID showed up where the year goes, then I would have a problem. In that case I'd probably make a new route for the show action to include the date in that too.
@Ted, LOL. Actually I whipped this up right after the event last night. That's why I sound tired.
@nicolash, good idea! I'll add that. Thanks for the suggestion!
Have any thoughts about how (and more importantly, when) to use method_missing in any of your upcoming screencasts? I guess maybe just a little metaprogramming in general might be nice, since that's an important thing to understand in Ruby. Just a thought...
Ryan, that was incredibly useful, thanks. I never would have thought of it, but it makes perfect sense upon seeing it. You don't sound nearly as tired as I felt!
@Jose, hmm, it should work as long as that route is near the top of your routes.rb file. But, I haven't tested this very much, I usually use named routes. Here's an example of that:
For those looking for Ryan's ugly hack for the by_date method check it out here:
http://railsforum.com/viewtopic.php?id=3741
I found this today and it presented a clever way to accomplish this functionality. It may not be pretty or rails-ish Ryan, but it gets the job done!
Thanks for all the casts Ryan - I check here when I don't know something, before I check anyway else. I subscribed today to your feed in iTunes, so I'll make sure I get the votes going.
def find_by_date
@posts = blog.posts.by_year(params[:year])
if params[:month]
@posts = @posts.by_month(params[:month])
@posts = @posts.by_day(params[:day]) if params[:day]
end
end
Thanks for this one Ryan!
It would be nice to see the code for by_date method too though.
Also what happens if the id of the article has 4 characters in it? Any other method of escaping from this?
Cheers.
@ekolguy
I guess it shouldnt matter because what the route is aiming to do is to stop the show action being interpreted as a date (for the index action)
I'm telling Jack you were working on a new episode while he was slaving over your Rails Rumble project ;-)
Thanks for your screencasts.
Maybe you should consider a Tag "routing" for these editions...
070_custom_routes
046_catch_all_route
035_custom_rest_actions
034_named_routes
@Ekolguy, I would but the by_date method is such an ugly hack that I don't want to show it. Maybe I'll improve it and turn it into an episode some day.
Regarding the routing, The name of the action shows up in the URL so there's no conflict. If I were using restful routes where the article ID showed up where the year goes, then I would have a problem. In that case I'd probably make a new route for the show action to include the date in that too.
@Ted, LOL. Actually I whipped this up right after the event last night. That's why I sound tired.
@nicolash, good idea! I'll add that. Thanks for the suggestion!
Have any thoughts about how (and more importantly, when) to use method_missing in any of your upcoming screencasts? I guess maybe just a little metaprogramming in general might be nice, since that's an important thing to understand in Ruby. Just a thought...
Ryan, that was incredibly useful, thanks. I never would have thought of it, but it makes perfect sense upon seeing it. You don't sound nearly as tired as I felt!
Hy Ryan,
please how can we do a link to an article like /articles/2007/10/12/id_of_article
Thanks a lot.
@Jose, you can do a link by passing the parameters like this:
link_to "Article", :controller => 'articles', :year => 2007, :month => 10, :day => 12
Thanks Ryan for your answer, but I have a problem, if I do:
link_to 'article', :controller => "articles", :year => '2007', :month => '10')
I get:
http://localhost:3000/articles?year=2007&month=10
Not:
http://localhost:3000/articles/2007/
10
And my route file is:
map.connect 'articles/:year/:month', :controller => 'articles',
:year => nil, :month => nil
Is this correct?
@Jose, hmm, it should work as long as that route is near the top of your routes.rb file. But, I haven't tested this very much, I usually use named routes. Here's an example of that:
http://pastie.caboo.se/106752
I explain named routes more in this episode:
http://railscasts.com/episodes/34
For those looking for Ryan's ugly hack for the by_date method check it out here:
http://railsforum.com/viewtopic.php?id=3741
I found this today and it presented a clever way to accomplish this functionality. It may not be pretty or rails-ish Ryan, but it gets the job done!
Thanks for all the casts Ryan - I check here when I don't know something, before I check anyway else. I subscribed today to your feed in iTunes, so I'll make sure I get the votes going.
Ryan Bates, Hi. :)
Man, I have one problem. :\
look: http://pastie.caboo.se/174710
How are your code by_date?
[]s
tks.
For all who are interested, this is what I did:
In the controller:
def find_by_date
@posts = blog.posts.by_year(params[:year])
if params[:month]
@posts = @posts.by_month(params[:month])
@posts = @posts.by_day(params[:day]) if params[:day]
end
end
=================
In the model:
named_scope :by_year, lambda { |year| {:conditions => ["YEAR(created_at) = ?", year]} }
named_scope :by_month, lambda { |month| {:conditions => ["MONTH(created_at) = ?", month]} }
named_scope :by_day, lambda { |day| {:conditions => ["DAY(created_at) = ?", day]} }
=================
I'd like to move more of the code into the model, I just haven't thought of a good way yet.
I wouldn't make this routes because 2006 can be id article so I won't open this article for example.