Rails为什么我得到未定义的方法

时间:2012-07-26 02:58:20

标签: ruby-on-rails ruby ruby-on-rails-3 refinerycms

我正在尝试创建一个用户可以输入电子邮件的表单,然后将其保存到数据库中。我希望这个表单在每个页面的页脚中。

我通过rails scaffolding生成了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>

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

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

1 个答案:

答案 0 :(得分: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| %>