2012-09-17 64 views
0

我在刪除和顯示用戶記錄時遇到問題。Ruby on Rails:路由錯誤

這裏是我的routes.rb

FinalApp::Application.routes.draw do 


resources :users 
devise_for :users, :controllers => { :registrations => 'admin' } 



    resources :projects 
    match "search" => "projects#search", :as => :search 

    root :to => 'projects#index' 
end 

這裏是我的管理控制器:

class AdminController < ApplicationController 
def index 
    @users = User.all 



respond_to do |format| 
     format.html # index.html.erb 
     format.json { render :json => @users } 
    end 
    end 

def create 

    @user = User.new(params[:user]) 



    respond_to do |format| 
     if @user.save 
     format.html { redirect_to @user, notice: 'User was successfully created.' } 
     format.json { render json: @user, status: :created, location: @user } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @user.errors, status: :unprocessable_entity } 
     end 
    end 
    end 




    # GET /users/1 
    # GET /users/1.json 
    def show 
    @user = User.find(params[:id]) 
@user_user_id = params[:id] 

    respond_to do |format| 
     format.html # show.html.erb 
     format.json { render json: @user } 
    end 
    end 

    # GET /users/new 
    # GET /users/new.json 
    def new 
    @user = User.new 

    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @user } 
    end 
    end 

    # GET /users/1/edit 
    def edit 
    @user = User.find(params[:id]) 
    end 

    # POST /users 
    # POST /users.json 



    # PUT /users/1 
    # PUT /users/1.json 
    def update 
    @user = User.find(params[:id]) 

    respond_to do |format| 
     if @user.update_attributes(params[:user]) 
     format.html { redirect_to @user, notice: 'User was successfully updated.' } 
     format.json { head :no_content } 
     else 
     format.html { render action: "edit" } 
     format.json { render json: @user.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /users/1 
    # DELETE /users/1.json 
    def destroy 
    @user = User.find(params[:id]) 
    @user.destroy 

    respond_to do |format| 
     format.html { redirect_to users_url } 
     format.json { head :no_content } 
    end 
    end 
end 

這是我的觀點:

<%= stylesheet_link_tag "admin" %> 
<body> 
<div id ="title1">Admin</div> 

<div class ="menu"></div> 
<div id ="section3"> 
<table id = "mytable"> 
<table border = "1"> 
<tr> 
    <th>Username </th> 


    <th>Email</th> 
    <th>First Name</th> 
     <th>Last Name</th> 
<th>Admin?</th> 

    <th></th> 
    <th></th> 
    <th></th> 

    </tr> 
    <%= link_to "New User", admin_new_path %><br /> 

<% @users.each do |t| %> 
    <tr> 

    <td><%= t.username %></td> 

    <td><%= t.email %></td> 
    <td><%= t.firstname %></td> 

    <td><%= t.lastname %></td> 
<td><%= t.admin %></td> 


    <td><%= link_to 'Show', t %></td> 
    <td> <%= button_to "Delete", t, method: :delete, data: { confirm: 'Are you sure?' } %></td> 

    </tr> 
<% end %> 
</table></br> 

</body> 

</html> 

我可以顯示用戶的數據庫,但當我去刪除一條記錄。我得到這個錯誤No route matches [DELETE] "https://stackoverflow.com/users/11"。我是新手,因此請在試圖幫助時記住這一點。提前致謝。

編輯:這裏是我的路線=>

admin_index GET /admin(.:format)    admin#index 
         POST /admin(.:format)    admin#create 
       new_admin GET /admin/new(.:format)   admin#new 
       edit_admin GET /admin/:id/edit(.:format)  admin#edit 
        admin GET /admin/:id(.:format)   admin#show 
         PUT /admin/:id(.:format)   admin#update 
         DELETE /admin/:id(.:format)   admin#destroy 
     new_user_session GET /users/sign_in(.:format)  devise/sessions#new 
      user_session POST /users/sign_in(.:format)  devise/sessions#create 
    destroy_user_session DELETE /users/sign_out(.:format)  devise/sessions#destroy 
      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)  admin#cancel 
     user_registration POST /users(.:format)    admin#create 
    new_user_registration GET /users/sign_up(.:format)  admin#new 
    edit_user_registration GET /users/edit(.:format)   admin#edit 
         PUT /users(.:format)    admin#update 
         DELETE /users(.:format)    admin#destroy 
       projects GET /projects(.:format)   projects#index 
         POST /projects(.:format)   projects#create 
      new_project GET /projects/new(.:format)  projects#new 
      edit_project GET /projects/:id/edit(.:format) projects#edit 
       project GET /projects/:id(.:format)  projects#show 
         PUT /projects/:id(.:format)  projects#update 
         DELETE /projects/:id(.:format)  projects#destroy 
        search  /search(.:format)    projects#search 
        root  /       projects#index 

EDIT2:這是我的routes.rb文件應該怎麼看過。使用耙路我可以改變路徑來解決我的問題。

FinalApp::Application.routes.draw do 

    # website home 
    root :to => 'projects#index' 

    # devise sessions (NB does not use admin/users controller) 
    devise_for :users, :controllers => { :registrations => 'users' } 

    # normal controllers 
    resources :users 
    resources :projects 

    # custom routes 
    match "search" => "projects#search", :as => :search 

end 

回答

1

您已經結束了兩個航線通往管理#破壞:

DELETE /users(.:format)    admin#destroy 
DELETE /admin/:id(.:format)   admin#destroy 

,但你有相匹配/用戶/ XX用戶提供了一個可選的格式沒有路由,只是它匹配刪除一條路由/位。你有一條匹配/ admin/11的路由,所以如果你嘗試過它可以工作,但是我會盡量簡化一些東西。

你真的需要在設計資源上指定一個控制器嗎?你想要在那裏重寫什麼,因爲你已經結束了與負載的路線(如取消),導致無處和一些衝突...

嘗試一個更簡單的路由定義(NB這需要重命名您的AdminController UsersController相反,我會遵守這個約定,因爲它會讓你的生活更輕鬆,並匹配您的其他URL,所以你最終用戶/ 1等,而不是管理/ 1)

FinalApp::Application.routes.draw do 

    # website home 
    root :to => 'projects#index' 

    # devise sessions (NB does not use admin/users controller) 
    devise_for :users 

    # normal controllers 
    resources :users 
    resources :projects 

    # custom routes 
    match "search" => "projects#search", :as => :search 

end 

那就不要耙路線,以確保你明白路線指向的地方。你需要它說的路線(NB的:ID位):

DELETE /users/:id(.:format)   users#destroy 

或者如果你喜歡一個管理控制器(並願意理清定製路由)潛水前

DELETE /admin/:id(.:format)   admin#destroy 

也許成這雖然你可以通過軌道路線導向閱讀,因爲它可能清除一些東西給你:

http://guides.rubyonrails.org/routing.html

+0

我希望用戶能夠登錄,如果他們已被授予管理員身份,他們可以訪問網站的管理員部分,在那裏可以創建,編輯和刪除新用戶。我會給你一個建議。謝謝。 – Jazz

+0

謝謝,現在完美。更新我的問題以顯示答案。 – Jazz

+0

雖然可以嘗試命名空間某些動作/控制器,但我會建議在/ admin下使用一些用戶動作而不是名稱空間,而只是爲某些動作添加冗餘檢查,以確保只有那些用戶才能執行重要動作。隨着您的應用程序的增長,您將需要多種限制(由用戶狀態(管理員,讀者,其他?))。看看這個cancan,或者建立你自己的更簡單的角色系統。 –

2

您應該添加resources :usersroutes.rb。無論如何,您始終可以在控制檯中查看rake routes以查看可用路線。

請注意,您定義路線admin的方式並不完全正確。並非所有東西都是get。例如,創建admin將是post。最簡單的方法就是使用像resources :admins這樣的東西。

+0

我已經加入我的路線給你看。添加'資源:用戶'似乎只是加載了更多的路線,並沒有解決問題。 – Jazz

+0

您可能需要將其添加到'devise_for'塊之外。 –

+0

我已經移動了資源:devise_for塊以上的用戶 – Jazz

1

這似乎是Devise的一個非常標準的用法,唯一的區別是Controller的名稱。因此,設計路由唯一需要的是:

devise_for :users, :controllers => { :registrations => 'admin' } 

另外,刪除所有「get'admin/*'」條目。當您在REST環境中工作時,並非每個HTTP方法都是GET。 Here is one article that discusses REST methods.

+0

我已經在您的建議後更新了我的問題。仍然不能解決我的問題 – Jazz

+0

因爲您已經在使用Devise,所以您不需要手動公開:users資源。設計負責你所有的路線生成。因此,您的整個路線文件應該只有一行設計,一行用於根路徑,一行用於項目資源,一行用於「搜索」路線匹配。 – nevern02