Rails为什么我获得了未定义的方法
-
11-12-2019 - |
题
我正在尝试创建一个用户可以在其电子邮件中键入的表单,它将将其保存到DB。我想要在每个页面的页脚中的表格。
我已经通过Rails脚手架生成了新闻稿。 现在我在my / 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
. 解决方案
它看起来像炼油厂挂载路线(不确定这是正确的术语),它看不到正常路由。
从此链接 https://groups.google.com/forum/#!主题/炼油厂-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| %>
. 不隶属于 StackOverflow