2014-09-01 21 views
0

我想在我的控制器來定義一個方法send如何在Rails控制器中定義一個名爲send的方法?

class InvoicesController < ApplicationController 

    def send 
    end 

end 

然而,當我這樣做,我得到一個錯誤wrong number of arguments (2 for 0)

所以我認爲send是Rails中的保留字?

無論如何,在我的控制器中定義一個send方法可能是一種可能的解決方法。

感謝您的任何想法。

+3

'send'是ruby中的內置方法。選擇另一個名字。例如,「send_invoice」。或者'創建',如果你想成爲RESTful。 – 2014-09-01 17:55:20

+0

@SergioTulentsev +1 http://teachmetocode.com/articles/ruby-on-rails-accessing-controller-methods-from-your-view/ – RAJ 2014-09-01 17:56:50

+0

堆棧跟蹤到底是什麼錯誤? – 2014-09-01 18:20:59

回答

1

你不能在ruby中調用方法'send'。發送是一個對象的方法

發送(*參數)公共

調用由符號標識的方法,通過它 指定任何參數。如果名稱發送衝突 與obj中的現有方法,您可以使用發送

Send method

+1

這不是保留字。 – 2014-09-01 18:06:55

+0

你是對的。更正 – 2014-09-01 18:10:20

0

的原因,你不能在控制器中send方法是send已經在Ruby對象類的方法。

但是,從稍微不同的角度提出問題,我可以看到在這種情況下爲方法指定具體名稱的主要原因是在路由中獲取該名稱,例如:http://localhost:3000/invoices/send或者用於RESTful資源/invoices/123/send

在這種情況下,您可以調用任何您喜歡的方法,並添加一條名爲send的路線指向您的方法。

所以,在你的控制器:

class InvoicesController < ApplicationController 

    def send_invoice 
    ... 
    end 

end 

然後在config/routes.rb

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

或爲發票資源:

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

我會考慮調用它postdeliver代替

相關問題