2011-12-15 6 views
0

레일스를 처음 사용하고웨어 하우스를 관리하기위한 웹 앱을 만들었습니다. 다음과 같은 문제가 있습니다 : "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 %> 
이다

업데이트, 편집, 표시, 색인과 같은 다른 "동작"은 잘 작동하지만 요소를 삭제하면 웹 서버 콘솔에서 오류가 발생하지 않지만 요소는 데이터베이스에서 제거되지 않습니다.

답변

1

당신은했습니다 : 라인의 끝에서

<td><%= link_to 'Destroy', position, :confirm => 'Are you sure?', :method => :delete %></td> 

당신은 jquery-rails로드 havn't는 :method => :destroy

+0

나는 그랬지만 여전히 문제는 남아 있습니다! 나는 '파괴'행동을 포함하는 각 컨트롤러와 뷰에 대해 동일한 문제를 겪어 왔다는 것을 알았다. – Marco

1

에 의해 :method => :delete를 교체합니다. 브라우저에서 DELETE 작업을 지원하지 않으면 jquery-rails으로 에뮬레이트됩니다.

당신의 Gemfile에이 줄을 추가

gem 'jquery-rails' 

을 실제로 jQuery를 사용하는 가정!

다음은 jquery-ujs 스크립트 (https://github.com/rails/jquery-ujs/blob/master/src/rails.js)에서 가져온 것입니다. 프로젝트에 이것이 없다면,

// Handles "data-method" on links such as: 
// <a href="https://stackoverflow.com/users/5" data-method="delete" rel="nofollow" data-confirm="Are you sure?">Delete</a> 
handleMethod: function(link) { 
    var href = link.attr('href'), 
    method = link.data('method'), 
    target = link.attr('target'), 
    csrf_token = $('meta[name=csrf-token]').attr('content'), 
    csrf_param = $('meta[name=csrf-param]').attr('content'), 
    form = $('<form method="post" action="' + href + '"></form>'), 
    metadata_input = '<input name="_method" value="' + method + '" type="hidden" />'; 

    if (csrf_param !== undefined && csrf_token !== undefined) { 
    metadata_input += '<input name="' + csrf_param + '" value="' + csrf_token + '" type="hidden" />'; 
    } 

    if (target) { form.attr('target', target); } 

    form.hide().append(metadata_input).appendTo('body'); 
    form.submit(); 
}, 
관련 문제