I've been spending a little time lately on updating The Secret App to remove cruft (Login / User Engine being the two main sources of said cruft). Now that I'm no longer locked into an old revision of edge, I have noticed that Rails's ActiveRecord behavior seems to have changed a bit. Before, I could get away with saying @widget = current_user.widgets.create!(params[:widget])
Now, that gets me a big fat nil in @widget.user_id. Unfortunately,@widget = current_user.widgets.new(params[:widget])
gives me the same darned thing. The only way I could get the user_id to fill properly was with@widget = current_user.widgets.create(params[:widget])
Maybe it's something I am doing wrong, but I haven't found it yet. I wouldn't put much effort into it, but I really like the clean syntax I get from #create! and so it has gotten under my skin. Any ideas? Anyone? Bueller?
It is almost, but not quite, completely unlike tea.
Thursday, February 22, 2007
create vs. create! vs. new in Rails
Posted by
Jim Kane
at
1:18 PM
0
comments
Labels: rubyonrails
Tuesday, December 05, 2006
Good reference for RESTful Rails
I love a good cheat sheet; anything to condense a big ol' reference site into a page or so that I can flip to when I need it. However, no one has made a good cheat sheet (or even a good reference, period) for the new RESTful Rails stuff. The best one-post reference I've found is simply_restful in Rails Core on David Goodlad's blog. Plenty of bloggers rah-rahed about simply_restful and gave a few examples of how to set it up, but David includes something unique: a table of all the auto-generated named routes and how to use them. I swear, I must have spent half an hour looking for exactly that information. Ah, sweet spoonfeeding! Thanks again to Mr. Goodlad for putting that out there.
Posted by
Jim Kane
at
11:32 AM
0
comments
Labels: rubyonrails
Sunday, October 01, 2006
A small change to Rails routing
A recent change in the way Rails deals with incoming requests caught me a bit off guard. I was used to being all loosey-goosey with my routes, like saying
/categories/show/34-Detachable-Widgets.html
and having Rails happyily translate that into a request for /categories/show/34. However, when I caught up to edge a few days ago, my tests started failing. Wah? Further investigation revealed that the culprit was that flagrant .html hanging there on the end (why? because I can). After a bit of digging on the Rails mailing list, I finally found my buried treasure: a post by Jeremy Kemper mentioning that Rails now considers . a "URL separator." Ergo, your route
:controller/:action/:id
becomes
:controller/:action/:id.:format
and all's right with the world again. This allows us to force a format just by using it as the extension (i.e. trigger a specific response via responds_to), so I don't mind the change. I just wish someone had mentioned it a bit more loudly... like I'm doing now.
Posted by
Jim Kane
at
11:20 AM
0
comments
Labels: rubyonrails