0

我认为这个问题最终可能会成为更多的通用帮助,但我在尝试删除回形针项目时遇到了问题。

当我点击我的按钮时,我只是得到 --- No route matches [POST] "/expenses/3" -- 也许这是调用方法的错误方式?

在此先感谢,代码如下。

这是我的查看按钮,我只是复制了我的删除按钮,并将控制器方法更改为新的。

            <%= link_to raw('<i class="icon-trash icon-white"> </i>'), 
                          expense_item, method: :destroy_receipt, 
                          data: { confirm: 'Are you sure delete receipt?' }, 
                          class: "btn btn-mini btn-danger" %>

在我的控制器中

def destroy_receipt
  @expense = Expense.find(params[:id])
  @expense.receipt.destroy
  redirect_to expense_path
end

我的模型

class Expense < ActiveRecord::Base
  attr_accessible :amount, :expense_date, :description, :is_billable, :mileage, 
                                :pay_method, :project_id, :type_id, :on_site, :receipt

    belongs_to :project, foreign_key: :project_id
    belongs_to :expense_type, foreign_key: :type_id

  has_attached_file :receipt, :styles => { :medium => "300x300>", :small => "100x100>" }
4

1 回答 1

1

你是对的,这不是正确的方法。

该方法的正确参数:关键是 POST、GET、PUT、DELETE

你会想要这样的东西:

link_to 'hi' , '/urlforhi/:id', :method=>:post

然后你必须在 routes.rb 中有一条路线:

post '/urlforhi/:id' => 'yourcontroller#hi'

然后在控制器/yourcontroller.rb

def hi
  @thing = Thing.find(params[:id])
end

:method 的参数指示使用哪个 HTTP VERB。

请注意,默认方法是 get,所以这也可以:

link_to 'hi' , '/urlforhi/:id'

然后你必须在 routes.rb 中有一条路线:

get '/urlforhi/:id' => 'yourcontroller#hi'

或更常见的

match '/urlforhi/:id' => 'yourcontroller#hi'
于 2012-08-24T17:58:17.127 回答