Rails3 Engine - 控制器范围位于引擎模块内部,由客户端应用程序返回加载错误
我刚刚创建了我的第一个引擎。它添加了一些新路线,如下所示:
Rails.application.routes.draw do
scope :module => 'contact' do
get "contact", :to => 'contacts#new'
get "contact/send_email", :to => 'contacts#send_email', :as => 'send_email'
end
end
然后,在 /websites/Engines/contact/app/controllers/contacts_controller.rb 中,我有:
module Contact
class ContactsController < ApplicationController
# Unloadable marks your class for reloading between requests
unloadable
def new
@contact_form = Contact::Form.new
end
def send_email
@contact_form = Contact::Form.new(params[:contact_form])
if @contact_form.valid?
Notifications.contact(@contact_form).deliver
redirect_to :back, :notice => 'Thank you! Your email has been sent.'
else
render :new
end
end
end
end
我将其加载到客户端应用程序的控制台中,以向自己证明一些基础知识正在快速工作出现此加载错误(然后我通过在浏览器中重现该问题来确认):
ruby-1.8.7-p302 > Contact::Form.new
=> #<Contact::Form:0x2195b70>
ruby-1.8.7-p302 > app.contact_path
=> "/contact"
ruby-1.8.7-p302 > r = Rails.application.routes; r.recognize_path(app.contact_path)
LoadError: Expected /websites/Engines/contact/app/controllers/contacts_controller.rb to define ContactsController
就这样; /contact 访问引擎的contacts_controller.rb,但控制器位于Contact 模块内部这一事实使其无法识别。
我做错了什么?
I just created my first engine. It adds a couple of new routes like so:
Rails.application.routes.draw do
scope :module => 'contact' do
get "contact", :to => 'contacts#new'
get "contact/send_email", :to => 'contacts#send_email', :as => 'send_email'
end
end
Then, in /websites/Engines/contact/app/controllers/contacts_controller.rb, I have:
module Contact
class ContactsController < ApplicationController
# Unloadable marks your class for reloading between requests
unloadable
def new
@contact_form = Contact::Form.new
end
def send_email
@contact_form = Contact::Form.new(params[:contact_form])
if @contact_form.valid?
Notifications.contact(@contact_form).deliver
redirect_to :back, :notice => 'Thank you! Your email has been sent.'
else
render :new
end
end
end
end
I loaded it up in the client app's console to prove to myself some basics were working and quickly got this load error (which I then confirmed by reproducing the issue in a browser):
ruby-1.8.7-p302 > Contact::Form.new
=> #<Contact::Form:0x2195b70>
ruby-1.8.7-p302 > app.contact_path
=> "/contact"
ruby-1.8.7-p302 > r = Rails.application.routes; r.recognize_path(app.contact_path)
LoadError: Expected /websites/Engines/contact/app/controllers/contacts_controller.rb to define ContactsController
And there you have it; /contact gets to the engine's contacts_controller.rb but the fact the controller is inside the module Contact makes it unrecognizable.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的
app/controllers/contacts_controller.rb
实际上定义的是Contact::ContactsController
,而不是 Rails 期望的ContactsController
。问题出在你的路线上,它们应该这样定义:
Your
app/controllers/contacts_controller.rb
is actually defining theContact::ContactsController
, not theContactsController
that Rails expects.The problem is with your routes, they should be defined like this:
感谢@ryan-bigg 和@nathanvda,他们的回答共同为我解决了这个问题。简而言之,我最终使用了以下路线:
使用以下控制器:
但似乎最后的部分是@nathanvda的建议,将contacts_controller从: 移动
到
谢谢你们的帮助!
Thanks to @ryan-bigg and @nathanvda their answers in conjunction fixed this issue for me. In short, I ended up using the following routes:
with the following controller:
but what seemed to be the final piece was @nathanvda's suggestions to move the contacts_controller from:
to
Thank you both for your help!