我目前正在嘗試爲一個項目組合一個畫廊系統與軌道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
嵌套資源不工作正常的資源,來看看:http://guides.rubyonrails.org/routing.html#creating-paths-and-urls-from-objects – Pigueiras 2013-03-11 19:16:36