2012-07-26 74 views
2

我想创建一个表单,用户可以键入他们的电子邮件,它会将其保存到数据库。我希望这个表格在每一页的页脚中。Rails为什么我得到未定义的方法

我已经通过铁轨脚手架生成了NewsletterSignup。

现在我有这样的代码在我/app/views/refinery/_footer.html.erb:

<%= form_for(@newsletter_signup) do |f| %> 
    <div class="field"> 
    <%= f.label :email %><br /> 
    <%= f.text_field :email %> 
    </div> 
    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

,当我尝试并加载网页我得到这个错误:

NoMethodError in Refinery/pages#home 

Showing /Users/tomcaflisch/Sites/PersonalTrainingKT/app/views/refinery/_form.html.erb where line #1 raised: 

undefined method `newsletter_signups_path' for #<#<Class:0x007fb84c769ba0>:0x007fb84c08d110> 

为什么该方法未定义?如果我运行耙路线我可以看到它的存在:

newsletter_signups GET /newsletter_signups(.:format)   newsletter_signups#index 
         POST /newsletter_signups(.:format)   newsletter_signups#create 
new_newsletter_signup GET /newsletter_signups/new(.:format)  newsletter_signups#new 
edit_newsletter_signup GET /newsletter_signups/:id/edit(.:format) newsletter_signups#edit 
    newsletter_signup GET /newsletter_signups/:id(.:format)  newsletter_signups#show 
         PUT /newsletter_signups/:id(.:format)  newsletter_signups#update 
         DELETE /newsletter_signups/:id(.:format)  newsletter_signups#destroy 

的routes.rb

PersonalTrainingKT::Application.routes.draw do 

    resources :newsletter_signups 

    # This line mounts Refinery's routes at the root of your application. 
    # This means, any requests to the root URL of your application will go to Refinery::PagesController#home. 
    # If you would like to change where this extension is mounted, simply change the :at option to something different. 
    # 
    # We ask that you don't use the :as option here, as Refinery relies on it being the default of "refinery" 
    mount Refinery::Core::Engine, :at => '/' 
end 

我也创建在application_controller一个新的对象,因为这简报注册将可在每一页上:

class ApplicationController < ActionController::Base 
    protect_from_forgery 

    before_filter :instantiate_newsletter_signup 

    def instantiate_newsletter_signup 
    @newsletter_signup = NewsletterSignup.new 
    end 
end 
+0

可以看到你们的路线文件? – ctcherry 2012-07-26 03:01:03

+0

我已经张贴了我的路线,除了炼油厂的路线 - 其中有一大堆那些。 – Catfish 2012-07-26 03:03:08

+0

我认为我所需要做的就是将form_for专门用于我的newsletter_signups_controller对吗? – Catfish 2012-07-26 03:03:58

回答

5

它看起来像由于refinerycms装载路线(不知道这是否正确的术语),它看不到正常的路线。

从这个链接https://groups.google.com/forum/#!topic/refinery-cms/5k7Co4D1bVI我想通了,我需要改变

<%= form_for(@newsletter_signup, :url => newsletter_signups_path, :method => :post) do |f| %> 

<%= form_for(@newsletter_signup, :url => main_app.newsletter_signups_path, :method => :post) do |f| %> 
+0

不错的一个,很高兴你得到它排序:) – 2012-07-26 04:15:15

+0

好的工作!这有点超出正常的Rails约定 – ctcherry 2012-07-26 05:02:42

+0

实际上,对于像Refinery这样的挂载引擎来说,这是正常的Rails约定。 – parndt 2012-07-26 06:02:00

相关问题