我目前正在尝试为带有 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