I'm trying to set up routes in Rails 3 that look like:
/items/:category/:name/
It's pretty easy to do a match to set this up, and then generate the URL with the following:
item_path(:category => @item.category, :name => @item.name)
But is there an开发者_开发问答y way to set it up so that item_path @item and form_for @item will work automatically, so I don't have to pass the category every time?
Thanks!
Not really no. I would suggest defining a to_params (note the 's') method on Item as follows:
def to_params
{:category => category, :name => name}
end
And then calling it like so item_path(@item.to_params)
. If you hack things to default to this I can guarantee you'll run into situations in which you don't want it.
精彩评论