Jhorra
Jhorra

Reputation: 6321

Error adding custom route to controller

I've rewritten my question to be more accurate. I have a bankaccounts controller / model.

I have the following method on my controller

def search
  #@ledgeritems = Ledgeritem.where("bankaccount_id = ? and transactiondate >= ? and transactiondate < ?", params[:bankaccount_id], params[:startdate], params[:enddate])
  @bankaccount = Bankaccount.find(params[:bankaccount_id])
  respond_to do |format|
    format.js { render :partial => "bankaccount/bankledger" }
  end
end

I've made two attempts to call this.

Attempt 1

Route for attempt 1

resources :bankaccounts do
    post "search"
end

This shows the following route when I do a rake

bankaccount_search POST   /bankaccounts/:bankaccount_id/search(.:format) bankaccounts#search

Javascript for calling attempt 1

$.ajax({
    type: "POST",
    url: "/bankaccounts/" + bank_account_id + "/search.js",
    data: $('#edit_bankaccount_' + bank_account_id).serialize(),
    success: function (result, status) {
        $('#bank_ledger_div').html(result);
    }
});

This calls the correct route on my controller, but the server sees it as a PUT instead of a POST and returns a 404.

Attempt 2

Route for attempt 2

resources :bankaccounts do
  collection do
    post "search"
  end
end

This shows the following route when I do a rake

search_bankaccounts POST   /bankaccounts/search(.:format)       bankaccounts#search

Javascript for calling attempt 2

$.ajax({
    type: "POST",
    url: "/bankaccounts/search.js",
    data: $('#edit_bankaccount_' + bank_account_id).serialize(),
    success: function (result, status) {
        $('#bank_ledger_div').html(result);
    }
});

This calls the update route but still showing as a PUT command. In Firebug I see a 500 error with the following result

Couldn't find Bankaccount with id=search

Upvotes: 0

Views: 69

Answers (3)

wless1
wless1

Reputation: 3549

Usually this error means you're making a GET request instead of a POST request.

For example:

GET /bankaccounts/search is assumed to be requesting the SHOW page for a bankaccount with ID = search

While

POST /bankaccounts/search would correctly hit your action.

Edit:

resources :bankaccounts do
  collection do
    post "search"
  end
end

Is correct as well. Now I'm noticing that you are doing this to get your data:

data: $('#edit_bankaccount_' + bank_account_id).serialize()

that form likely has a hidden field in it, put there by rails, with name='_method' and value='PUT'. That is what is convincing rails that your POST is really a PUT. You'll need to remove that from the serialized data in order to correctly post the form.

Upvotes: 2

Mark Swardstrom
Mark Swardstrom

Reputation: 18070

The URL is expecting the format

/bankaccounts/:bankaccount_id/search

Is the error coming from this method? Could /bankaccounts/search be matching another route?

Upvotes: 1

user1003545
user1003545

Reputation: 2118

If you want the /search url to be used without specifying an id, you should declare it as a collection action :

resources :bankaccounts do
  collection do
    post "search"
  end
end

You can check the routes defined in your app with the rake routes command, to ensure that you defined what you meant.

Upvotes: 1

Related Questions