我是新来的rails,并且我创建了一个web应用程序来管理我的仓库。 我有以下问题:当我点击“销毁”按钮(见下面附件)时,相关对象不是从mysql2数据库中delate。任何人都可以帮助我?Rails 3.1应用程序无法销毁mysql2对象
这是控制器
`
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
这是模型
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 %>
其他“行动”,如更新,编辑,显示,索引工作正常,但当我摧毁一个元素,它不会给我任何错误的Web服务器控制台,但元素不会从数据库中删除。
我已经这样做了,但问题依然存在!我已经看到,我对每个包含'destroy'操作的控制器和视图都有相同的问题 – Marco 2011-12-15 15:38:00