views:

509

answers:

2

I'm a beginner in ruby-on-rails and I spent my last hour trying to do the following thing:

I have a ruby-on-rails application - the blog with posts and categories. I want to have another URL for the posts (I would like to have http://localhost:3000/news instead of http://localhost:3000/posts) First I tried to replace the controller and classes from Posts to News, but I gave up(because of the annoyng singular-plural thing). Then in my I replaced map.resources :posts (case 1) to

  map.resources :news, :controller => "posts"     #case 2

or

  map.resources :posts, :as => 'news'             #case 3

in routes.rb as I saw on some websites. It doesn't work either.

How can I do this?


EDIT:

the output of rake routes is (only first lines):

for case 1 and 3:

               posts GET    /posts                           {:action=>"index", :controller=>"posts"}
     formatted_posts GET    /posts.:format                   {:action=>"index", :controller=>"posts"}
                     POST   /posts                           {:action=>"create", :controller=>"posts"}
                     POST   /posts.:format                   {:action=>"create", :controller=>"posts"}
            new_post GET    /posts/new                       {:action=>"new", :controller=>"posts"}
  formatted_new_post GET    /posts/new.:format               {:action=>"new", :controller=>"posts"}
           edit_post GET    /posts/:id/edit                  {:action=>"edit", :controller=>"posts"}
 formatted_edit_post GET    /posts/:id/edit.:format          {:action=>"edit", :controller=>"posts"}
                post GET    /posts/:id                       {:action=>"show", :controller=>"posts"}
      formatted_post GET    /posts/:id.:format               {:action=>"show", :controller=>"posts"}
                     PUT    /posts/:id                       {:action=>"update", :controller=>"posts"}
                     PUT    /posts/:id.:format               {:action=>"update", :controller=>"posts"}
                     DELETE /posts/:id                       {:action=>"destroy", :controller=>"posts"}
                     DELETE /posts/:id.:format               {:action=>"destroy", :controller=>"posts"}

the output for case 2:

           news_index GET    /news                            {:action=>"index", :controller=>"posts"}
 formatted_news_index GET    /news.:format                    {:action=>"index", :controller=>"posts"}
                      POST   /news                            {:action=>"create", :controller=>"posts"}
                      POST   /news.:format                    {:action=>"create", :controller=>"posts"}
             new_news GET    /news/new                        {:action=>"new", :controller=>"posts"}
   formatted_new_news GET    /news/new.:format                {:action=>"new", :controller=>"posts"}
            edit_news GET    /news/:id/edit                   {:action=>"edit", :controller=>"posts"}
  formatted_edit_news GET    /news/:id/edit.:format           {:action=>"edit", :controller=>"posts"}
                 news GET    /news/:id                        {:action=>"show", :controller=>"posts"}
       formatted_news GET    /news/:id.:format                {:action=>"show", :controller=>"posts"}
                      PUT    /news/:id                        {:action=>"update", :controller=>"posts"}
                      PUT    /news/:id.:format                {:action=>"update", :controller=>"posts"}
                      DELETE /news/:id                        {:action=>"destroy", :controller=>"posts"}
                      DELETE /news/:id.:format                {:action=>"destroy", :controller=>"posts"}

I have errors in case 2, because in my sourcecode I don't have edit_news, I have for example <%= link_to 'Edit', edit_post_path(post) %>

A: 

Your first step was good: replace all Post(s)* classes to News* classes. Calling your model News and controller NewsController should not be a problem. Make sure you find all occurrences, also in the code (also things like post_path should be replaced with news_path).

Next, modify your route to

map.resources :news

in order to use the NewsController instead of the PostsController. And it should work.

Note: Do not forget to restart your webserver.

Veger
I did like you said, but I had to add `:singular => :news_instance` in `routes.rb` and replace `new_news_path` to `new_news_instance_path`. There are still some problems left to fix. But I don't know why the example with `productos` on this page doesn't work: http://api.rubyonrails.org/classes/ActionController/Resources.html#M000522
True Soft
+1  A: 

The word "news" has ambiguity between singular (member) and plural (collection) names. You could try working around this, but you might just end up confusing yourself, and rails. (Should "news_path" expect an id parameter for the member, or is it the collection path? What is "a news"?)

Let's stick to calling them posts:

map.resources :posts, :as => "news", :singular => "news"
  • Your route will be "/news"
  • Your controller will be PostsController
  • Your path helpers will be posts_path, post_path, edit_post_path, etc.

In other words, in all cases, you will call the resources "posts" but they will show up in the routes under "/news/*".

Andrew Vit
You are right. I now avoid names with the same singular and plural form. When I want to redefine them (or set non-english names), I add new inflections in `config/initializers/inflections.rb`
True Soft