2011-03-26 44 views
0

我剛剛創建了我的第一個引擎。它增加了一些新的航線,像這樣:Rails3引擎 - 控制器範圍內的引擎模塊返回客戶端的負載錯誤應用程序

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中使其無法識別。

我在做什麼錯?

回答

0

感謝@瑞恩 - 比格和@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 

謝謝你們的幫助!

4

您的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 
+0

沒錯。我希望我的引擎的ContactController範圍在Contact模塊中,因此除非您確切知道您在做什麼,否則不會有碰撞的機會。所以我想我的問題應該是我如何得到軌道期望Contact :: ContactsController而不是僅僅ContactsController?我認爲這就是我將我的聯繫路線放在範圍內所做的事情:module =>'contact' – ynkr 2011-03-26 23:05:35

+0

@ynkr更新了答案 – 2011-03-27 00:04:32

+0

另外請注意,您應該將控制器放在正確的文件夾中:'app/controllers/contact/contacts_controller.rb ' – nathanvda 2011-03-27 22:23:28

相關問題