3

I have this in routes.rb

root :to => "posts#index"

  devise_for :users,  :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }

  resources :users, :only => :show

  resources :boards 

  resources :posts do
  resources :comments
  end 

namespace :users do
 resources :posts do
  get :posts, :on => :member
 end
 resources :boards do  
  get :boards, :on => :member
 end  
end      

rake routes:

  boards_users_board GET    /users/boards/:id/boards(.:format)        {:action=>"boards", :controller=>"users/boards"}
        users_boards GET    /users/boards(.:format)                   {:action=>"index", :controller=>"users/boards"}
                    POST    /users/boards(.:format)                   {:action=>"create", :controller=>"users/boards"}
     new_users_board GET    /users/boards/new(.:format)               {:action=>"new", :controller=>"users/boards"}
    edit_users_board GET    /users/boards/:id/edit(.:format)          {:action=>"edit", :controller=>"users/boards"}
         users_board GET    /users/boards/:id(.:format)               {:action=>"show", :controller=>"users/boards"}

If I put this link with 2 parameters:

<% @posts.each do |post| %>
   <%= link_to post.board.name, users_board_path(post.user, post.board) %>
<% end %>

I get the next url with a dot:

http://localhost:3000/users/boards/hyperrjas.board-2

  • hyperrjas is the user_id that I have put with slug :username.
  • I use namespace because are nested resources and I have a panel for users.

My question is: How can I change the dot for slash / in my generated url? should look and work as follows:

http://localhost:3000/users/boards/hyperrjas/board-2

iwasrobbed
  • 46,496
  • 21
  • 150
  • 195
hyperrjas
  • 10,666
  • 25
  • 99
  • 198

1 Answers1

0

Move the user routes below the nested ones. Rails will "catch" the upper routes first.

Your problem is really with the route generation (and some ordering too). If you need to access the users boards, you don't need a namespaced route but a nested one.

If you need /users/:user_id/boards and /users/:user_id/boards/:id you'll need the nested route. Notice that in the second route there are 2 params (user_id and id). If you use a namespaced route, you'll only "need" one (the board id). The second argument would be the format. Notice that in the output from rake routes you only "need" 1 param.

Try the next route to see if it works.

  resources :users do
    # This will give you /users/:user_id/posts
    # and /users/:user_id/posts/:post_id
    # among others
    resources :posts
  end
Hock
  • 5,784
  • 2
  • 19
  • 25
  • Thanks for your reply, I do not understand what you want me, you could specify a little more? thanks – hyperrjas Jan 10 '12 at 17:52
  • I'm not really sure if you should be using `namespace` anyway. You should use nested resources there. (I'll post another answer) – Hock Jan 10 '12 at 17:56
  • Thank you, with resources working fine but if I works with resources the controller its **boards** and I want working with controller **users/boards**. This is my problem If I work with resources work with controller **boards** and I want working with controller **users/boards** – hyperrjas Jan 10 '12 at 18:31
  • Any fix for this problem? Thank you – hyperrjas Jan 11 '12 at 11:58
  • The problem it's fixed :D. I had add in routes.rb the next: `resources :users do resources :posts, :controller => 'users/posts' resources :boards, :controller => 'users/boards' end` Thanks – hyperrjas Jan 11 '12 at 14:10