路由库操作

时间:2013-03-11 19:07:56

标签: ruby-on-rails ruby-on-rails-3 rails-activerecord rails-routing

我目前正在尝试为带有rails 3的项目组建一个图库系统。我觉得我应该从头开始进行更多的控制和学习。但是,当我尝试从http://localhost:3000/admin/albums/33/images删除图片时,删除按钮会转到http://localhost:3000/admin/albums/1/images。似乎删除按钮获取所选图像的ID,而不是将其附加到URL的末尾,它将用它替换url中的相册ID。我需要删除按钮才能调用http://localhost:3000/admin/albums/33/images/1来删除该图像。我无法想象我的生活。

查看

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

控制器

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

路线

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 Image < ActiveRecord::Base
    attr_accessible :title, :description, :image_name
    has_and_belongs_to_many :albums
end

相册模型

class Album < ActiveRecord::Base
    attr_accessible :title, :description
    has_and_belongs_to_many :images,  :dependent => :destroy
    validates :title, :description, :presence => true
end

1 个答案:

答案 0 :(得分:1)

您需要更改admin_album_images_path,作为嵌套资源,您需要指定这两种资源。

<%= button_to "Delete", admin_album_images_path(image.album,image), :method => :delete, :style => "display: block;" %>