【问题标题】:NoMethodError in FrontPages#home when I try to add a form当我尝试添加表单时,FrontPages#home 中的 NoMethodError
【发布时间】:2014-06-06 23:36:57
【问题描述】:

我很确定这是一个非常菜鸟的错误,但我不明白为什么我不能在不导致 NoMethodError 的情况下直接将表单添加到我的主页。

这是我目前的设置,它工作得很好。

view/front_pages/home.html.erb(我的主页)

<!--declare :title to be SHOULD I GET THIS-->
<% provide(:title, "SHOULD I GET THIS") %>
<div class = "container">
    <h1>Should I really spend money on this?</h1>
    <h2>Let's crunch in some numbers and find out...</h2>
    <%= link_to "Get Started", calculate_path, class: "btn btn-info " %>
</div>

view/users/new.html.erb(我的表单现在所在的位置)

<% provide(:title, "Calculate")%>
<div class = "form-group container">
    <%= simple_form_for @user do |form|%>
        <%= form.error_notification%>
        <%= form.input :price%>
        <%= form.input :wallet%>
        <%= form.button :submit, "Submit", class: "submit"%>
    <%end%>
</div>

控制器/users_controller.rb

class UsersController < ApplicationController
    def new
        @user = User.new
    end

end

但是,我希望表单直接出现在我的主页上,这样用户就不需要单击额外的按钮来访问表单。我试过了

查看/front_pages/home.html.erb

<!--declare :title to be SHOULD I GET THIS-->
<% provide(:title, "SHOULD I GET THIS") %>
<div class = "container">
    <h1>Should I really spend money on this?</h1>
    <h2>Let's crunch in some numbers and find out...</h2>
    <%= link_to "Get Started", calculate_path, class: "btn btn-info " %>
</div>
<div class = "form-group container">
    <%= simple_form_for @user do |form|%>
        <%= form.error_notification%>
        <%= form.input :price%>
        <%= form.input :wallet%>
        <%= form.button :submit, "Submit", class: "submit"%>
    <%end%>
</div>

它返回 NoMethodError。我以为是因为我没有在front_page控制器中初始化@user变量,所以我尝试了

class FrontPagesController < ApplicationController
        def new
            @user = User.new
        end
    def home
    end
end

但它仍然不起作用。我认为这是关于 MVC 的一些概念,我仍然不太了解。我的代码有什么问题,下次我应该记住什么,以免犯同样的错误?

PS:我使用 simple_form gem 来生成我的表单

编辑:错误消息是未定义方法 `model_name' for NilClass:Class

【问题讨论】:

  • 发布完整的错误和导致它的行。
  • 对不起,我已经添加了错误信息

标签: ruby-on-rails ruby simple-form


【解决方案1】:

错误很简单:

NilClass:Class 的未定义方法 `model_name'

此错误表示您尝试对未填充任何数据的变量调用model_name 方法。 没有调用这个方法; form_for 是 - 意味着您基本上需要在控制器中声明 @user,正如您正确指出的那样:

class FrontPagesController < ApplicationController

    def new
        @user = User.new
    end
    def home
    end
 end

--

@user

问题在于您正在使用 home 操作 - 您在 new 操作中声明了您的 @user 变量。这意味着它不会被设置,因为它不会被调用。

你最好这样做:

class FrontPagesController < ApplicationController
    def home
       @user = User.new
    end
 end

这将使@user 在您的home 操作中可用,这应该会为您解决错误!

【讨论】:

  • 太棒了,这很有效,非常感谢!!所以可以肯定地说我不能简单地在 FontPageController 中添加一个 def 新方法,因为我的文件名为 home.html.erb,因此我需要在 def home 中声明所有变量?
  • 没有控制器动作是相互独立的——你可以有尽可能多的动作;但只声明每个变量都需要的变量
【解决方案2】:

您需要为表单指定操作

<%= form_for :user, url: { action: create } do |f| %>

或者,如果您为用户定义了资源,则可以使用路径助手

<%= form_for :user, url: user_path do |f| %>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-08
    • 1970-01-01
    • 2011-07-22
    • 2023-01-24
    • 2015-04-10
    • 1970-01-01
    • 1970-01-01
    • 2015-02-16
    相关资源
    最近更新 更多