What is the correct way to use a more friendly name as a replacement within standard Rails 2.0 URLs (routes). For example, I have a table blog_posts and a model called BlogPost. But that makes me URL for ID #1 look like this http://localhost:3000/blog_posts/1 I would rather have the url look like http://localhost:3000/blog/1 How do I modify the routes.rb file to make this happen? Thanks, Kelly Greer kellygreer1-GMWn+aNOponQT0dZR+AlfA@public.gmane.org change nospam to yahoo --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
# this entry seems to get me part of the way, but then I get errors map.connect ''blog/:action/:id'', :controller => ''blog_posts'' errors: undefined method `blog_post_path'' for #<ActionView::Base:0x358f928> Thanks, Kelly On Apr 28, 3:06 pm, kellygreer1 <kellygre...-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:> What is the correct way to use a more friendly name as a replacement > within standard Rails 2.0 URLs (routes). > > For example, I have a table blog_posts and a model called BlogPost. > But that makes me URL for ID #1 look like thishttp://localhost:3000/blog_posts/1 > > I would rather have the url look likehttp://localhost:3000/blog/1 > > How do I modify the routes.rb file to make this happen? > > Thanks, > Kelly Greer > kellygre...-GMWn+aNOponQT0dZR+AlfA@public.gmane.org > change nospam to yahoo--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
map.resources :blog, :controller => "blog_posts", :singlular => "blog" should work. it will create a restful resource. see http://www.railsbrain.com/api/rails-2.0.2/doc/index.html?a=M000313&name=resources for more info. On Apr 28, 3:37 pm, kellygreer1 <kellygre...-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:> # this entry seems to get me part of the way, but then I get errors > > map.connect ''blog/:action/:id'', :controller => ''blog_posts'' > > errors: > undefined method `blog_post_path'' for #<ActionView::Base:0x358f928> > > Thanks, > Kelly > > On Apr 28, 3:06 pm, kellygreer1 <kellygre...-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote: > > > What is the correct way to use a more friendly name as a replacement > > within standard Rails 2.0 URLs (routes). > > > For example, I have a table blog_posts and a model called BlogPost. > > But that makes me URL for ID #1 look like thishttp://localhost:3000/blog_posts/1 > > > I would rather have the url look likehttp://localhost:3000/blog/1 > > > How do I modify the routes.rb file to make this happen? > > > Thanks, > > Kelly Greer > > kellygre...-GMWn+aNOponQT0dZR+AlfA@public.gmane.org > > change nospam to yahoo--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
huh... I get the same error. Showing blog_posts/index.html.erb where line #17 raised: undefined method `blog_post_path'' for #<ActionView::Base:0x36f53d0> Anything else I should do? Kelly On Apr 28, 4:14 pm, Kyle Banker <kyleban...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> map.resources :blog, :controller => "blog_posts", :singlular => "blog" > > should work. it will create a restful resource. > > seehttp://www.railsbrain.com/api/rails-2.0.2/doc/index.html?a=M000313&na... > for more info. > > On Apr 28, 3:37 pm,kellygreer1<kellygre...-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote: > > > # this entry seems to get me part of the way, but then I get errors > > > map.connect ''blog/:action/:id'', :controller => ''blog_posts'' > > > errors: > > undefined method `blog_post_path'' for #<ActionView::Base:0x358f928> > > > Thanks, > > Kelly > > > On Apr 28, 3:06 pm,kellygreer1<kellygre...-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote: > > > > What is the correct way to use a more friendly name as a replacement > > > within standard Rails 2.0 URLs (routes). > > > > For example, I have a table blog_posts and a model called BlogPost. > > > But that makes me URL for ID #1 look like thishttp://localhost:3000/blog_posts/1 > > > > I would rather have the url look likehttp://localhost:3000/blog/1 > > > > How do I modify the routes.rb file to make this happen? > > > > Thanks, > > > Kelly Greer > > > kellygre...-GMWn+aNOponQT0dZR+AlfA@public.gmane.org > > > change nospam to yahoo--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
kellygreer1 wrote:> huh... I get the same error. > Showing blog_posts/index.html.erb where line #17 raised: > undefined method `blog_post_path'' for #<ActionView::Base:0x36f53d0> > > Anything else I should do? > > KellyDid you restart your server? :) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Daniel Waite wrote:> kellygreer1 wrote: >> huh... I get the same error. >> Showing blog_posts/index.html.erb where line #17 raised: >> undefined method `blog_post_path'' for #<ActionView::Base:0x36f53d0> >> >> Anything else I should do? >> >> Kelly > > Did you restart your server? :)Also, type ''rake routes'' to get a list of all the routing methods generating by your routes definitions. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
restarted the server, and that made no difference. here is the output of ''rake routes'' blog_index GET /blog {:controller=>"blog_posts", :action=>"index"} formatted_blog_index GET /blog.:format {:controller=>"blog_posts", :action=>"index"} POST /blog {:controller=>"blog_posts", :action=>"create"} POST /blog.:format {:controller=>"blog_posts", :action=>"create"} new_blog GET /blog/new {:controller=>"blog_posts", :action=>"new"} formatted_new_blog GET /blog/new.:format {:controller=>"blog_posts", :action=>"new"} edit_blog GET /blog/:id/edit {:controller=>"blog_posts", :action=>"edit"} formatted_edit_blog GET /blog/:id/edit.:format {:controller=>"blog_posts", :action=>"edit"} blog GET /blog/:id {:controller=>"blog_posts", :action=>"show"} formatted_blog GET /blog/:id.:format {:controller=>"blog_posts", :action=>"show"} PUT /blog/:id {:controller=>"blog_posts", :action=>"update"} PUT /blog/:id.:format {:controller=>"blog_posts", :action=>"update"} DELETE /blog/:id {:controller=>"blog_posts", :action=>"destroy"} DELETE /blog/:id.:format {:controller=>"blog_posts", :action=>"destroy"} /:controller/:action/:id /:controller/:action/:id.:format Thanks, Kelly On Apr 28, 5:14 pm, Daniel Waite <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Daniel Waite wrote: > > kellygreer1 wrote: > >> huh... I get the same error. > >> Showing blog_posts/index.html.erb where line #17 raised: > >> undefined method `blog_post_path'' for #<ActionView::Base:0x36f53d0> > > >> Anything else I should do? > > >> Kelly > > > Did you restart your server? :) > > Also, type ''rake routes'' to get a list of all the routing methods > generating by your routes definitions. > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
the method will no longer be ''blog_posts_path'' it''ll now be ''blog_path.'' err, fix: map.resources :blogs, :controller => "blog_posts", :singlular => "blog" note: ''blogs'' is an unusual name for a resource. The real resource your referencing is a post. Ideally, you''d use: map.resources :posts, :controller => "blog_posts" which would give you post_path, etc. great article on REST: http://www.b-simple.de/download/restful_rails_en.pdf On Apr 28, 5:14 pm, Daniel Waite <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Daniel Waite wrote: > > kellygreer1 wrote: > >> huh... I get the same error. > >> Showing blog_posts/index.html.erb where line #17 raised: > >> undefined method `blog_post_path'' for #<ActionView::Base:0x36f53d0> > > >> Anything else I should do? > > >> Kelly > > > Did you restart your server? :) > > Also, type ''rake routes'' to get a list of all the routing methods > generating by your routes definitions. > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
well I would like to always call my objects BlogPosts (or singular BlogPost) And I do want my table to be named something more than ''posts'' So blog_posts works great until you get to the URLs. Too bad Rails leaves the underscore character in the URL. Its funny, Grails has an issue around mulitword table names too. In Grails the issue is camelcasing the URL. So I would end up with a URL like this: http://localhost/blogPost/ So what do you most people do in Rails.... avoid multi-word table names? Think I know how to fix this stuff now - hack the method names. Thanks for everyones help. Kelly On Apr 28, 5:33 pm, Kyle Banker <kyleban...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> the method will no longer be ''blog_posts_path'' > > it''ll now be ''blog_path.'' > > err, fix: > > map.resources :blogs, :controller => "blog_posts", :singlular => > "blog" > > note: ''blogs'' is an unusual name for a resource. The real resource > your referencing is a post. > > Ideally, you''d use: > > map.resources :posts, :controller => "blog_posts" > > which would give you post_path, etc. > > great article on REST:http://www.b-simple.de/download/restful_rails_en.pdf > > On Apr 28, 5:14 pm, Daniel Waite <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > > > Daniel Waite wrote: > > > kellygreer1 wrote: > > >> huh... I get the same error. > > >> Showing blog_posts/index.html.erb where line #17 raised: > > >> undefined method `blog_post_path'' for #<ActionView::Base:0x36f53d0> > > > >> Anything else I should do? > > > >> Kelly > > > > Did you restart your server? :) > > > Also, type ''rake routes'' to get a list of all the routing methods > > generating by your routes definitions. > > -- > > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
kellygreer1 wrote:> well I would like to always call my objects BlogPosts (or singular > BlogPost) > And I do want my table to be named something more than ''posts'' > So blog_posts works great until you get to the URLs. > Too bad Rails leaves the underscore character in the URL. > > Its funny, Grails has an issue around mulitword table names too. > In Grails the issue is camelcasing the URL. > So I would end up with a URL like this: > http://localhost/blogPost/ > > So what do you most people do in Rails.... avoid multi-word table > names?In general - yes. When I read you had a table called "blog_posts" my mind went immediately to a Has And Belongs To Many association (HABTM), which joins two other models - Blogs and Posts. Not knowing much about the application you are writing it is difficult to say for sure, but it appears you will be supporting multiple blogs (one or more per user), each containing multiple posts. The models would reflect this relationship and so therefore so would the tables in the database. eg. ----Models class Blog < ActiveRecord::Base has_many :posts end class Post < ActiveRecord::Base belongs_to :blog end ----Routes map.resources :blogs :has_many => :posts> > Think I know how to fix this stuff now - hack the method names. > > Thanks for everyones help. > KellyAndrew -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
ok.... let me ask a really dumb question. I''m really new to this. So is the naming convention to never use an underscore unless you are doing a many-to-many relationship? So if I wanted to work with BlogPost objects Is the best method to call the table - blogposts and the model - Blogpost ? Should I give up on the idea of being able to have a model called BlogPost (with the capital P)? Seems like a major hassle. The reason I''m asking is Post/Posts just seems so generic. Kelly On Apr 28, 6:43 pm, Andrew Skegg <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> kellygreer1 wrote: > > well I would like to always call my objects BlogPosts (or singular > > BlogPost) > > And I do want my table to be named something more than ''posts'' > > So blog_posts works great until you get to the URLs. > > Too bad Rails leaves the underscore character in the URL. > > > Its funny, Grails has an issue around mulitword table names too. > > In Grails the issue is camelcasing the URL. > > So I would end up with a URL like this: > >http://localhost/blogPost/ > > > So what do you most people do in Rails.... avoid multi-word table > > names? > > In general - yes. > > When I read you had a table called "blog_posts" my mind went immediately > to a Has And Belongs To Many association (HABTM), which joins two other > models - Blogs and Posts. > > Not knowing much about the application you are writing it is difficult > to say for sure, but it appears you will be supporting multiple blogs > (one or more per user), each containing multiple posts. The models > would reflect this relationship and so therefore so would the tables in > the database. > > eg. > ----Models > > class Blog < ActiveRecord::Base > has_many :posts > end > > class Post < ActiveRecord::Base > belongs_to :blog > end > > ----Routes > map.resources :blogs :has_many => :posts > > > > > Think I know how to fix this stuff now - hack the method names. > > > Thanks for everyones help. > > Kelly > > Andrew > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
kellygreer1 wrote:> ok.... let me ask a really dumb question. > I''m really new to this.That''s OK - I am not an expert either, just a keen amateur.> > So is the naming convention to never use an underscore unless you are > doing a > many-to-many relationship?I don''t think there is anything wrong with underscores (anyone?), but the name itself is ambiguous to rails, hence the need for all the fancy footwork to make the routes work.> > So if I wanted to work with BlogPost objects > Is the best method to call the table - blogposts > and the model - Blogpost ?Yes. This is closer to the conventions rails uses, which equates to less work for you and more happiness.> Should I give up on the idea of being able to have a > model called BlogPost (with the capital P)?The capital "P" in the model/table is not an issue. Neither is the name until you get to exposing it to the user via a route. Rails expects the mapping to be simple and straight forward. Since you are stepping away from the conventions Rails is punishing you - it does that. Whenever things seems difficult in Rails you are most likely running against the grain. I have found the easiest way is to name your controllers following what you want the user to type. So /blog/ would require a "blogs" controller. This makes the routes easy (map.resources blogs). The models don''t really matter that much, but you may want to have posts belong to something else other than blogs at some stage in the future. With a name like "blogposts" you kind of box yourself in.> Seems like a major hassle. > The reason I''m asking is Post/Posts just seems so generic.What''s wrong with generic? :)> > Kelly > > > > On Apr 28, 6:43 pm, Andrew Skegg <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---