2013-03-13 66 views
0

當我在我的index.html.erb頁面上爲我的圖像調用delete操作時,它引用了正確的id等,但它沒有正確地爲它們排序。該網址應該看起來像http://localhost:3000/admin/albums/33/images/1,但它顯示爲http://localhost:3000/admin/albums/33/images.1。我知道它與。:格式有關,但我不知道如何解決它。當我把這個刪除操作,admin_album_images_path([@album, image]),它導致http://localhost:3000/admin/albums/33/1/images.rails 3.2刪除對象的url順序

路線

Admin::Application.routes.draw do 
    get "albums/index" 

    get "dashboard/index" 

    namespace :admin do 
    root :to => "dashboard#index" 
    resources :dashboard 
    resources :albums do 
     resources :images 
    end 
    get "admin/album" 
    end 
    get "logout" => "sessions#destroy", :as => "logout" 
    get "login" => "sessions#new", :as => "login" 
    get "signup" => "users#new", :as => "signup" 
    # resources :users 
    resources :basic 
    root :to => "basic#index" 

控制器

class Admin::ImagesController < ApplicationController 
    def index 
     @album = Album.find(params[:album_id]) 
     @image = @album.images(params[:id]) 
     @images = Image.all 
    end 
    def new 
     @album = Album.find(params[:album_id]) 
     @image = @album.images.new 
    end 
    def create 
     @album = Album.find(params[:album_id]) 
     @image = @album.images.build(params[:image]) 
     if @image.save 
      flash[:notice] = "Successfully added image!" 
      redirect_to [:admin, :albums] 
     else 
      render :action => 'new' 
     end 
    end 
    def show 
     @album = Album.find(params[:id]) 
     @image = @album.images(params[:id]) 
    end 
    def destroy 
     @album = Album.find(params[:album_id]) 
     @image = @album.images(params[:id]) 
     @image.destroy 
     redirect_to admin_albums_path 
    end 

end 

查看

<% @images.each do |image|%> 
    <%= image.title %> </br> 
    <%= image.description %> </br> 
    <%= image.image_name %> </br> 
    <%= button_to "Delete", admin_album_images_path(@album, image), :method => :delete, :style => "display: block; float: left;" %> 
    <%= debug @image %> 
<% end %> 

回答

1

你應該使用

admin_album_image_path(@album, image) 

注意,這兩個是單數。你也可以改變,要[:admin, @album, image],所以你不必擔心路線

的附加參數的名稱會默認成爲格式,以便

admin_album_image_path(@album, image, :csv) 

將被轉換爲

/admin/albums/1/images/1.csv 
+0

等我拿到了!你真是太神奇了......這個錯誤現在讓我煩惱了好幾天 – 2013-03-13 23:20:23