1

私は現在、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
4

1 に答える 1

1

を変更する必要がadmin_album_images_pathあります。ネストされたリソースとして、両方のリソースを指定する必要があります。

<%= button_to "Delete", admin_album_images_path(image.album,image), :method => :delete, :style => "display: block;" %>
于 2013-03-11T19:13:22.107 に答える