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
Dusty- The route generation is not fully baked yet. At this point you pass in a hash to fill in the missing parts of the routes. I''m open for suggestions about how this should work though. Cheers- -Ezra On Jul 18, 2007, at 11:23 PM, Dusty Doris wrote:> 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 > Merb-devel at rubyforge.org > http://rubyforge.org/mailman/listinfo/merb-devel-- Ezra Zygmuntowicz -- Founder & Ruby Hacker -- ez at engineyard.com -- Engine Yard, Serious Rails Hosting -- (866) 518-YARD (9273)
I''ve thought about it and I don''t have any objections to the current method. It keeps the code lean since you don''t need to decide if the url is part of a nested resource. Its really not too much extra work to send the needed hash, and one could always create a helper if they wanted something else. If I come up with any other ideas, I''ll be sure to send it this way to see what you guys think. Perhaps I''ll submit a patch for some code documentation? Maybe a simple commented out explanation to go above the method? I''ll take a look at some other comments and try to model those. Would that be welcome? Is the process for that to create a ticket in trac? Thanks On 7/19/07, Ezra Zygmuntowicz <ez at engineyard.com> wrote:> Dusty- > > The route generation is not fully baked yet. At this point you pass > in a hash to fill in the missing parts of the routes. I''m open for > suggestions about how this should work though. > > Cheers- > -Ezra > > On Jul 18, 2007, at 11:23 PM, Dusty Doris wrote: > > > 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 > > Merb-devel at rubyforge.org > > http://rubyforge.org/mailman/listinfo/merb-devel > > -- Ezra Zygmuntowicz > -- Founder & Ruby Hacker > -- ez at engineyard.com > -- Engine Yard, Serious Rails Hosting > -- (866) 518-YARD (9273) > > >
On Jul 20, 2007, at 11:10 AM, Dusty Doris wrote:> If I come up with any other ideas, I''ll be sure to send it this way to > see what you guys think. Perhaps I''ll submit a patch for some code > documentation? Maybe a simple commented out explanation to go above > the method? I''ll take a look at some other comments and try to model > those. Would that be welcome? Is the process for that to create a > ticket in trac? > > ThanksPlease do submit a patch! They''re very welcome, and yes, the proper way to submit it would be by creating a new ticket in trac. Cheers, Stephen