当我访问/contact
时,我得到:
ArgumentError (wrong number of arguments (given 1, expected 0)):
app/controllers/contact_controller.rb:6:in `send'
以下是config/routes.rb
的内容:
Rails.application.routes.draw do
root to: 'home#index'
get 'contact', to: 'contact#index'
post 'contact', to: 'contact#send'
end
是否包括get
和post
相同路径的路线会导致get
不被使用? ContactController
对index
和send
有空洞的行动,但index
似乎被忽略了。
我认为问题是Rails使用Object#send
按名称调用控制器方法,但你有自己的send
方法。在Rails路由系统内部,它知道它应该将GET /contact
路由到一个名为字符串'index'
的方法;代码看起来像这样:
controller_instance = an_instance_of_ContactController_from_somewhere
controller_method = 'index' # This string will be extracted from the `get 'contact', to: '...'` call in routes.rb
controller_instance.send(controller_method)
你应该能够将你的send
方法重命名为其他东西,更新你的routes.rb
,一切都应该没问题;例如:
post 'contact', to: 'contact#send_message'
然后在contact_controller.rb
:
def send_message
#...
end