gpt4 book ai didi

ruby-on-rails - Rails 为什么我得到未定义的方法

转载 作者:数据小太阳 更新时间:2023-10-29 08:33:24 25 4
gpt4 key购买 nike

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

我已经通过 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 routes 我可以看到它存在:

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

最佳答案

看起来由于 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| %>

关于ruby-on-rails - Rails 为什么我得到未定义的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11661688/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com