简体   繁体   中英

Username in urls - Ruby on Rails

I am working on a project which includes signup, login and users can post articles. However I want to include username of the user in article url. So I did like this in routes.rb

scope '/:username' do
resources :articles, :path => '/status',  only: [:create, :destroy, :show]
end

Now when I open project index which shows all articles, I am getting this error.

No route matches {:action=>"create", :controller=>"articles"} missing required keys: [:username]

Am I doing something wrong?

Edit: Here is the output of rake routes

Prefix Verb   URI Pattern                     Controller#Action
         users GET    /users(.:format)                users#index
               POST   /users(.:format)                users#create
      new_user GET    /users/new(.:format)            users#new
     edit_user GET    /users/:id/edit(.:format)       users#edit
          user GET    /users/:id(.:format)            users#show
               PATCH  /users/:id(.:format)            users#update
               PUT    /users/:id(.:format)            users#update
               DELETE /users/:id(.:format)            users#destroy
          root GET    /                               static_pages#home
          help GET    /help(.:format)                 static_pages#help
        signup GET    /signup(.:format)               users#new
         login GET    /login(.:format)                sessions#new
               POST   /login(.:format)                sessions#create
        logout DELETE /logout(.:format)               sessions#destroy
               GET    /                               users#index
               POST   /                               users#create
               GET    /new(.:format)                  users#new
               GET    /:id/edit(.:format)             users#edit
               GET    /:id(.:format)                  users#show
               PATCH  /:id(.:format)                  users#update
               PUT    /:id(.:format)                  users#update
               DELETE /:id(.:format)                  users#destroy
      archings POST   /:username/status(.:format)     articles#create
       arching GET    /:username/status/:id(.:format) articles#show
               DELETE /:username/status/:id(.:format) articles#destroy

Here is the code of routes.rb

Rails.application.routes.draw do
  root 'static_pages#home'
  get 'help' => 'static_pages#help'
  get 'signup' => 'users#new'
  get    'login'   => 'sessions#new'
  post   'login'   => 'sessions#create'
  delete 'logout'  => 'sessions#destroy'
  resources :users, shallow: true do
    resources :articles
   end
end

Although having really short URLs may be attractive you should not use a wildcard at the "lowest level" of your url schema - unless your application only really deals with a single type of resource. Adding a wildcard at the bottom level in your urls causes huge headaches since it will swallow every other route unless great care is taken.

I would really recommend that you use /users/:username/articles .

resources :users, shallow: true do
  resources :articles
end

This is a better restful design and it means that you do not have to blacklist usernames against a list of the other urls (and future urls) needed in your application.

Added:

The output of $ rake routes :

          Prefix Verb   URI Pattern                            Controller#Action
   user_articles GET    /users/:user_id/articles(.:format)     articles#index
                 POST   /users/:user_id/articles(.:format)     articles#create
new_user_article GET    /users/:user_id/articles/new(.:format) articles#new
    edit_article GET    /articles/:id/edit(.:format)           articles#edit
         article GET    /articles/:id(.:format)                articles#show
                 PATCH  /articles/:id(.:format)                articles#update
                 PUT    /articles/:id(.:format)                articles#update
                 DELETE /articles/:id(.:format)                articles#destroy
           users GET    /users(.:format)                       users#index
                 POST   /users(.:format)                       users#create
        new_user GET    /users/new(.:format)                   users#new
       edit_user GET    /users/:id/edit(.:format)              users#edit
            user GET    /users/:id(.:format)                   users#show
                 PATCH  /users/:id(.:format)                   users#update
                 PUT    /users/:id(.:format)                   users#update
                 DELETE /users/:id(.:format)                   users#destroy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM