私はレールが初めてで、私の倉庫を管理するためのWebアプリケーションを作成しました。 私は以下の問題を抱えています。「破棄」ボタン(下記参照)をクリックすると、関連オブジェクトはmysql2データベースから削除されません。誰でも私を助けることができますか?Rails 3.1アプリはmysql2オブジェクトを破壊できません
これは、このモデル
class Position < ActiveRecord::Base
has_many :warehouses
has_many :locks_on_warehouses, :class_name => "Warehouse", :foreign_key => "lock_by_position_id"
has_many :users
belongs_to :registry
端
であり、これは図
<h1>Listing positions</h1>
<table>
<tr>
<th>Name</th>
</tr>
<% for position in @positions %>
<tr>
<td><%=h position.name %></td>
<td><%= link_to 'Show', position %></td>
<td><%= link_to 'Edit', edit_position_path(position) %></td>
<td><%= link_to 'Destroy', position, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New position', new_position_path %>
あるコントローラ
`
class PositionsController < ApplicationController
#before_filter :check_count, :only => [:destroy]
# GET /positions
# GET /positions.xml
def index
@positions = Position.find(:all)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @positions }
end
end
# GET /positions/1
# GET /positions/1.xml
def show
@position = Position.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @position }
end
end
# GET /positions/new
# GET /positions/new.xml
def new
@position = Position.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @position }
end
end
# GET /positions/1/edit
def edit
@position = Position.find(params[:id])
end
# POST /positions
# POST /positions.xml
def create
@position = Position.new(params[:position])
respond_to do |format|
if @position.save
flash[:notice] = 'Position was successfully created.'
format.html { redirect_to(@position) }
format.xml { render :xml => @position, :status => :created, :location => @position }
else
format.html { render :action => "new" }
format.xml { render :xml => @position.errors, :status => :unprocessable_entity }
end
end
end
# PUT /positions/1
# PUT /positions/1.xml
def update
@position = Position.find(params[:id])
respond_to do |format|
if @position.update_attributes(params[:position])
flash[:notice] = 'Position was successfully updated.'
format.html { redirect_to(@position) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @position.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /positions/1
# DELETE /positions/1.xml
def destroy
@position = Position.find(params[:id])
@position.destroy
respond_to do |format|
format.html { redirect_to(positions_url) }
format.xml { head :ok }
end
end
private
def check_count
@position_to_destroy = Logicalwarehouse.find(params[:id])
if @position_to_destroy.warehouse.count > 0
flash[:notice] = "Non puoi cancellare una posizione in uso"
redirect_to :positions
end
end
end
あります3210
更新、編集、表示、索引などの他の「アクション」はうまく動作しますが、要素を破棄すると、Webサーバーコンソールにエラーは表示されませんが、要素はデータベースから削除されません。
私はこれを実行しましたが、まだ問題は残ります。私は、それぞれのコントローラーとビューに「破棄」アクションを含む同じ問題があることを確認しました – Marco