2011-12-15 49 views
0

我是新来的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服务器控制台,但元素不会从数据库中删除。

回答

1

您已经:

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

在生产线的末端通过:method => :destroy

+0

我已经这样做了,但问题依然存在!我已经看到,我对每个包含'destroy'操作的控制器和视图都有相同的问题 – Marco 2011-12-15 15:38:00

1

你没有带装jquery-rails更换:method => :deleteDELETE操作不受浏览器支持,它由jquery-rails模拟。

此行添加到您的Gemfile:

gem 'jquery-rails' 

你实际上是使用jQuery假设!

以下内容取自jquery-ujs脚本(https://github.com/rails/jquery-ujs/blob/master/src/rails.js)。如果在您的项目中不存在,DELETE将不起作用:

// 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(); 
},