routes.rb question

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@nospam.com change nospam to yahoo

# 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

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.

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

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? :slight_smile:

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? :slight_smile:

Also, type 'rake routes' to get a list of all the routing methods generating by your routes definitions.

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

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

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

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

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

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? :slight_smile: