简体   繁体   中英

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:

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. It yields a form builder object as the argument q .

<%= q.text_field :quiz_name %>

Here you are calling the text_field method on the form builder with the field named quiz_name . This means it will generate a text input, and call the quiz_name method on @quiz to find the current value.

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.

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 ? Or perhaps you just want to send a field that isn't connected to the Quiz model inside this form. (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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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