Rails3 Engine - 控制器范围位于引擎模块内部,由客户端应用程序返回加载错误

发布于 2024-10-27 08:10:40 字数 1430 浏览 1 评论 0原文

我刚刚创建了我的第一个引擎。它添加了一些新路线,如下所示:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请帮我爱他 2024-11-03 08:10:40

您的 app/controllers/contacts_controller.rb 实际上定义的是 Contact::ContactsController,而不是 Rails 期望的 ContactsController

问题出在你的路线上,它们应该这样定义:

Rails.application.routes.draw do
  scope :module => 'contact' do
    get "contact", :to => 'contact/contacts#new'
    get "contact/send_email", :to => 'contact/contacts#send_email', :as => 'send_email'
  end
end

Your app/controllers/contacts_controller.rb is actually defining the Contact::ContactsController, not the ContactsController that Rails expects.

The problem is with your routes, they should be defined like this:

Rails.application.routes.draw do
  scope :module => 'contact' do
    get "contact", :to => 'contact/contacts#new'
    get "contact/send_email", :to => 'contact/contacts#send_email', :as => 'send_email'
  end
end
可可 2024-11-03 08:10:40

感谢@ryan-bigg 和@nathanvda,他们的回答共同为我解决了这个问题。简而言之,我最终使用了以下路线:

Rails.application.routes.draw do
  scope :module => 'contact' do
    get  "contact", :to => 'contacts#new'
    post "contact/send_email", :to => 'contacts#send_email', :as => 'send_email'
  end
end

使用以下控制器:

module Contact
  class ContactsController < ApplicationController

    def new
      @contact_form = Contact::Form.new
    end

    def send_email
      @contact_form = Contact::Form.new(params[:contact_form])

      if @contact_form.valid?
        Contact::Mailer.contact_us(@contact_form).deliver
        redirect_to :back, :notice => 'Thank you! Your email has been sent.'
      else
        render :new
      end
    end

  end
end

但似乎最后的部分是@nathanvda的建议,将contacts_controller从: 移动

/app/controllers/contacts_controller.rb

/app/controllers/contact/contacts_controller.rb

谢谢你们的帮助!

Thanks to @ryan-bigg and @nathanvda their answers in conjunction fixed this issue for me. In short, I ended up using the following routes:

Rails.application.routes.draw do
  scope :module => 'contact' do
    get  "contact", :to => 'contacts#new'
    post "contact/send_email", :to => 'contacts#send_email', :as => 'send_email'
  end
end

with the following controller:

module Contact
  class ContactsController < ApplicationController

    def new
      @contact_form = Contact::Form.new
    end

    def send_email
      @contact_form = Contact::Form.new(params[:contact_form])

      if @contact_form.valid?
        Contact::Mailer.contact_us(@contact_form).deliver
        redirect_to :back, :notice => 'Thank you! Your email has been sent.'
      else
        render :new
      end
    end

  end
end

but what seemed to be the final piece was @nathanvda's suggestions to move the contacts_controller from:

/app/controllers/contacts_controller.rb

to

/app/controllers/contact/contacts_controller.rb

Thank you both for your help!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文