Atari2600
Atari2600

Reputation: 1239

link/button to delete paperclip - routing error

I think this question might end up being more rails generic help, but I am encountering a problem when trying to delete a paperclip item.

When I click my button, I simply get --- No route matches [POST] "/expenses/3" -- Perhaps this is the wrong way to call a method?

Thanks in advance, code below.

Here is my view button, I just copied my delete button, and changed the controller method to a new one.

            <%= 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" %>

and in my controller

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

my model

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>" }

Upvotes: 0

Views: 498

Answers (1)

RadBrad
RadBrad

Reputation: 7304

You are correct, that is not the correct way to go about it.

The proper arguments to the method: key are POST,GET,PUT,DELETE

You'd want something like:

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

Then you'd have to have a route in routes.rb:

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

Then in controllers/yourcontroller.rb

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

The argument to :method indicates which HTTP VERB to use.

Note that the DEFAULT method is get, so this would also work:

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

Then you'd have to have a route in routes.rb:

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

or more commonly

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

Upvotes: 1

Related Questions