undefined method `user_path'

后端 未结 2 1425
星月不相逢
星月不相逢 2021-02-01 03:44

I am trying to construct a users model manually (without using \'resources :users\' in the routes.rb file). My routes.rb file looks like this:

match \'/users/:id         


        
相关标签:
2条回答
  • 2021-02-01 04:44
    match '/users/:id', :to => 'users#show' 
    

    should be

    match '/users/:id', :to => 'users#show', :as => :user
    

    The :as parameter tells the router what to name the route as (You can then add _path or _url to whatever the :as parameter is).

    Also, any time you link directly to an ActiveRecord model (e.g. link_to user.email, user), it will try to turn user into user_path.

    0 讨论(0)
  • 2021-02-01 04:45

    For everyone who using rails 4 in your routes.rb post '/users', :to => 'users#create', :as => :user and you controller render json: @user, status: :created, location: @user_path

    0 讨论(0)
提交回复
热议问题