当我在我的 index.html.erb 图像页面上调用我的删除操作时,它引用了正确的 id 等,但它没有正确地在 url 中对它们进行排序。url 应该看起来像http://localhost:3000/admin/albums/33/images/1
,但它显示为http://localhost:3000/admin/albums/33/images.1
。我知道它与 .:format 有关,但我不确定如何修复它。当我把它放在删除操作中时admin_album_images_path([@album, image])
,它会导致http://localhost:3000/admin/albums/33/1/images.
路线
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 %>