레일스를 처음 사용하고웨어 하우스를 관리하기위한 웹 앱을 만들었습니다. 다음과 같은 문제가 있습니다 : "destroy"버튼 (아래 첨부 참조)을 클릭하면 관련 객체가 mysql2 데이터베이스에서 삭제되지 않습니다. 누구든지 나를 도울 수 있습니까? 레일 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 %>
이다
업데이트, 편집, 표시, 색인과 같은 다른 "동작"은 잘 작동하지만 요소를 삭제하면 웹 서버 콘솔에서 오류가 발생하지 않지만 요소는 데이터베이스에서 제거되지 않습니다.
나는 그랬지만 여전히 문제는 남아 있습니다! 나는 '파괴'행동을 포함하는 각 컨트롤러와 뷰에 대해 동일한 문제를 겪어 왔다는 것을 알았다. – Marco