简体   繁体   English

Ruby on Rails,方法未定义

[英]Ruby on Rails, method undefined

I'm very new to ruby on rails. 我是红宝石的新手。 I'm trying to make a text field to assign one of my variables (end_date), but I keep getting this error: 我正在尝试创建一个文本字段来分配我的变量之一(end_date),但我一直收到此错误:

undefined method `end_date' for #<Quiz:0x007fccd1e0f9c0>

Here's my code: 这是我的代码:

<%# Main Canvas where cardes places %>
<div class="column large-11" id="main">
  <%= form_for @quiz do |q| %>
    <%= q.label :quiz_name %>
    <%= q.text_field :quiz_name %>
    <%= q.label :end_date %>
    <%= q.text_field :end_date %>
    <%= hidden_field_tag 'selected', 'none' %>
    <%= q.hidden_field :classroom_id, value: @classroom_id%>

  <%= q.submit "Create Quiz", class: "expanded button" %>
  <% end %>
  <%= form_tag("/quiz/#{@classroom_id}/copy", method: "get") do %>
  <%= label :id, "ID" %>
  <%= text_field_tag "id", "" %>
  <%= submit_tag "Copy Quiz By ID", class: "expanded button" %>
  <% end %>

</div>

Let me break down how these different pieces relate to one-another, which hopefully will make this easier for you to troubleshoot. 让我分解一下这些不同部分之间的相互关系,希望它们可以使您更轻松地进行故障排除。

<%= form_for @quiz do |q| %>

Here you are invoking form_for to create a form bound to the @quiz object. 在这里,您要调用form_for来创建绑定到@quiz对象的表单。 It yields a form builder object as the argument q . 它产生一个表单生成器对象作为参数q

<%= q.text_field :quiz_name %>

Here you are calling the text_field method on the form builder with the field named quiz_name . 在这里,您正在使用名为quiz_name的字段在表单生成器上调用text_field方法。 This means it will generate a text input, and call the quiz_name method on @quiz to find the current value. 这意味着它会生成一个文本输入,并调用quiz_name的方法@quiz查找当前值。

So given that background, it should be clear why you are seeing this error: 因此,在这种背景下,应该清楚为什么会看到此错误:

<%= q.text_field :end_date %>

You are telling the form builder to call @quiz.end_date for the value of this field, but that method does not exist. 您正在告诉表单构建器调用@quiz.end_date作为该字段的值,但是该方法不存在。

You have not given enough code samples for us to determine why you expect this method to exist. 您没有为我们提供足够的代码示例来确定为什么希望这种方法存在。 Perhaps this is a field you've added to the quizzes table, but haven't yet run the migration? 也许这是您已添加到测验表中的字段,但尚未运行迁移? Is this supposed to be a virtual attribute on Quiz ? 这应该是Quiz的虚拟属性吗? Or perhaps you just want to send a field that isn't connected to the Quiz model inside this form. 或者,也许您只想发送此表单中未连接到Quiz模型的字段。 (You can do that with a separate set of helpers, in this case text_field_tag , that give you more flexibility in where the data comes from). (您可以使用单独的一组帮助器(在本例中为text_field_tag来完成此操作),以使您更加灵活地选择数据的来源)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM