我正在尝试创建一个表单,用户可以在其中输入他们的电子邮件并将其保存到数据库中。我希望在每一页的页脚中都有这个表格。
我已经通过 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>
为什么方法未定义?如果我运行 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
路线.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