I have a question about the usage of Merb::Controller#url. When I
have nested resources, requesting the new action works differently
than I expected. I was wondering what the intended usage is.
For example:
Say I have
r.resources :articles do |article|
article.resources :article_attachments
end
irb(main):002:0> show_routes
[:articles, "/articles"]
[:article_attachments, "/articles/:article_id/article_attachments"]
[:article_attachment, "/articles/:article_id/article_attachments/:id"]
[:edit_article_attachment, "/articles/:article_id/article_attachments/:id/edit"]
[:new_article_attachment, "/articles/:article_id/article_attachments/new"]
[:custom_new_article_attachment,
"/articles/:article_id/article_attachments/new/:action"]
[:article, "/articles/:id"]
[:edit_article, "/articles/:id/edit"]
[:new_article, "/articles/new"]
[:custom_new_article, "/articles/new/:action"]
=> nil
If I find an article:
irb(main):003:0> article = Article.find(1)
=> #<Article:0x341f2b4 @attributes={"intro"=>nil,
"updated_at"=>"2007-07-18 21:31:35", "title"=>"asdf", "body"=>nil,
"id"=>"1", "article_attachments_count"=>"2", "published_at"=>nil,
"created_at"=>"2007-07-18 21:31:35"}>
And an attachment of that article:
irb(main):004:0> attachment = article.article_attachments.find(1)
=> #<ArticleAttachment:0x3410b60 @attributes={"content_type"=>"asdf",
"updated_at"=>"2007-07-18 21:38:08", "public"=>"t", "id"=>"1",
"filename"=>"asdf", "article_id"=>"1", "position"=>"1",
"created_at"=>"2007-07-18 21:38:08"}>
My expectation was that I would pass the object I want to evaluate to
the url method. This works when I use edit_article_attachment. (note
I added some debug puts statements in there before and after the
SECTION_REGEXP sub)
irb(main):005:0> url(:edit_article_attachment,attachment)
--path before: /articles/:article_id/article_attachments/:id/edit
--path after: /articles/1/article_attachments/1/edit
=> "/articles/1/article_attachments/1/edit"
However, when having a new form on the page, I typically create the
new action with an empty instance of that model.
irb(main):006:0> new_attachment = article.article_attachments.new
=> #<ArticleAttachment:0x3406e94 @attributes={"content_type"=>nil,
"updated_at"=>nil, "public"=>true, "filename"=>nil, "article_id"=>nil,
"position"=>nil, "created_at"=>nil}, @new_record=true>
But, that model doesn't yet have the article_id attached to it, so the
url generation fails.
irb(main):007:0> url(:new_article_attachment,new_attachment)
--path before: /articles/:article_id/article_attachments/new
--path after: /articles//article_attachments/new
=> "/articles//article_attachments/new"
My expectation was that you should send the object that it is nested
from, (before I realized what that regex match was doing). But I see
that its looking for association id, rather than just the id, so that
also fails.
irb(main):008:0> url(:new_article_attachment,article)
--path before: /articles/:article_id/article_attachments/new
--path after: /articles//article_attachments/new
=> "/articles//article_attachments/new"
However, an easy work around is to simply pass it a hash with the
correct information in it.
irb(main):009:0> url(:new_article_attachment,{:article_id => article.id})
--path before: /articles/:article_id/article_attachments/new
--path after: /articles/1/article_attachments/new
=> "/articles/1/article_attachments/new"
Is that the intended behavior?
Thanks!
Dusty Doris
_______________________________________________
Merb-devel mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/merb-devel