Tintin81
Tintin81

Reputation: 10207

How to define a method called send in a Rails controller?

I am trying to define a method send in my controller:

class InvoicesController < ApplicationController

  def send
  end

end

However, I am getting an error wrong number of arguments (2 for 0) when I do this.

So I assume that send is a reserved word in Rails?

What could be a possible workaround for defining a send method in my controller anyway.

Thanks for any ideas.

Upvotes: 0

Views: 477

Answers (3)

Artur INTECH
Artur INTECH

Reputation: 7246

I would consider calling it post or deliver instead

Upvotes: 0

Shadwell
Shadwell

Reputation: 34774

The reason you can't have a send method in a controller is that send is already a method on the ruby Object class.

However, coming at the question from a slightly different angle, the main reason I can see to have a specific name for a method in this case is to get that name in the routing so that you can have, e.g.: http://localhost:3000/invoices/send or for a RESTful resource /invoices/123/send.

In which case you could call the method whatever you like and add a route named send to point at your method.

So, in your controller:

class InvoicesController < ApplicationController

  def send_invoice
    ...
  end

end

Then in config/routes.rb:

get 'invoices/send', to: 'invoices#send_invoice'

Or for an invoices resource:

resources :invoices do
  member :send, to: :send_invoice
end

Upvotes: 0

Sergio D. M&#225;rquez
Sergio D. M&#225;rquez

Reputation: 386

You cannot call a method 'send' in ruby. Send is an Object method

send(*args) public

Invokes the method identified by symbol, passing it any arguments specified. You can use send if the name send clashes with an existing method in obj.

Send method

Upvotes: 2

Related Questions