辅助方法生成

时间:2013-11-11 12:56:00

标签: ruby-on-rails ruby devise

我的问题是rails帮助器为我的用户模型User

生成错误的链接
<%= link_to "delete", user_registration_path(user), :method => :delete, :confirm => "You sure?" %>

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

我的routes.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',设计(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

2 个答案:

答案 0 :(得分: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} %>  

答案 1 :(得分:-1)

移除resource :user并将tandempractice路由移至resources :users

如果你想保留/user/tandem类似的路线,

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

您应该添加:删除用户资源路由声明。

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

并将link_to更改为users_path,如

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