-1

我的问题是关于 rails helpers 为我的用户模型 User 生成错误的链接

<%= link_to "delete", user_registration_path(user), :method => :delete, :confirm => "You sure?" %>

生成以下网址:whatever.com/users.1

我的路线.rb:

put "rooms/:id/close" => "rooms#close", :as => "close_room"

devise_for :users, path_names: {sign_in: "login", sign_out: "logout"},
controllers: {omniauth_callbacks: "omniauth_callbacks"}
get "home/index"

resource :user do
  # Route GET /user/tandem
  get 'tandem', :on => :collection
  get 'practice', :on => :collection

end

root :to => 'home#index'

resources :rooms
match '/party/:id', :to => "rooms#party", :as => :party, :via => :get
match '/users/find', :to => "users#find", :as => :find_users, :via => :get
# Sample resource route (maps HTTP verbs to controller actions automatically):
match '/users/edit/:id', :to => "users#edit", :as => :edit_user, :via => :get
resources :users, only: [:index ,:show, :edit, :update]

我正在使用 ruby​​ '1.9.3'、'rails'、'3.2.13'、devise(2.2.4)。编辑操作也会发生这种情况。编辑:我的路线文件是

 close_room PUT      /rooms/:id/close(.:format) rooms#close

 new_user_session GET      /users/login(.:format)                 devise/sessions#new
 user_session POST     /users/login(.:format)                 devise/sessions#create
 destroy_user_session DELETE   /users/logout(.:format)
 devise/sessions#destroy
 user_omniauth_authorize GET|POST /users/auth/:provider(.:format)
 omniauth_callbacks#passthru {:provider=>/facebook/}
 user_omniauth_callback GET|POST /users/auth/:action/callback(.:format) omniauth_callbacks#(?-mix:facebook)
 user_password POST     /users/password(.:format)              devise/passwords#create
 new_user_password GET      /users/password/new(.:format)          devise/passwords#new
 edit_user_password GET      /users/password/edit(.:format)         devise/passwords#edit
                     PUT      /users/password(.:format)                   devise/passwords#update
 cancel_user_registration GET      /users/cancel(.:format)                devise/registrations#cancel
 user_registration POST     /users(.:format)                       devise/registrations#create
 new_user_registration GET      /users/sign_up(.:format)               devise/registrations#new
edit_user_registration GET      /users/edit(.:format)                  devise/registrations#edit
                     PUT      /users(.:format)                       devise/registrations#update
                     DELETE   /users(.:format)                       devise/registrations#destroy
          home_index GET      /home/index(.:format)                  home#index
         tandem_user GET      /user/tandem(.:format)                 users#tandem
       practice_user GET      /user/practice(.:format)               users#practice
                user POST     /user(.:format)                        users#create
            new_user GET      /user/new(.:format)                    users#new
           edit_user GET      /user/edit(.:format)                   users#edit
                     GET      /user(.:format)                        users#show
                     PUT      /user(.:format)                        users#update
                     DELETE   /user(.:format)                        users#destroy
                root          /                                      home#index
               rooms GET      /rooms(.:format)                       rooms#index
                     POST     /rooms(.:format)                       rooms#create
            new_room GET      /rooms/new(.:format)                   rooms#new
           edit_room GET      /rooms/:id/edit(.:format)              rooms#edit
                room GET      /rooms/:id(.:format)                   rooms#show
                     PUT      /rooms/:id(.:format)                   rooms#update
                     DELETE   /rooms/:id(.:format)                   rooms#destroy
               party GET      /party/:id(.:format)                   rooms#party
          find_users GET      /users/find(.:format)                  users#find
               users GET      /users(.:format)                       users#index
                     POST     /users(.:format)                       users#create
                     GET      /users/new(.:format)                   users#new
                     GET      /users/:id/edit(.:format)              users#edit
                     GET      /users/:id(.:format)                   users#show
                     PUT      /users/:id(.:format)                   users#update
                     DELETE   /users/:id(.:format)                   users#destroy

4

2 回答 2

0

你应该改变

<%= link_to "delete", user_registration_path(user), :method => :delete, :confirm => "You sure?" %>

<%= link_to "delete", user_path(user), :method => :delete, :confirm => "You sure?" %>

user_registration_path与删除用户无关 - 事实上恰恰相反。

编辑

在看到您的rake routes输出后,似乎还有其他一些问题 - 有多种途径可以破坏用户 - 您应该努力纠正它,但以下内容肯定会起作用:

<%= link_to "delete", {controller: 'users', action: 'destroy', id: @user.id },
                      {method: :delete} %>  
于 2013-11-11T13:19:08.547 回答
-1

移除和resource :user移动路径到tandempracticeresources :users

如果你想保留/user/tandem这样的路线使用,

resource :user, only => [] do
  #tandem and practice routes as existing
end`

您应该将 :delete 添加到用户资源路由声明中。

resources :users, only: [:index ,:show, :edit, :update, :delete]

并将其更改link_tousers_path喜欢

link_to 'Destroy', users_path(user), method: :delete, data: { confirm: 'Are you sure?' }

于 2013-11-11T13:08:57.487 回答