gpt4 book ai didi

mysql - 在新 Controller 方法中使用 form_for 时未定义方法

转载 作者:行者123 更新时间:2023-11-30 00:15:05 26 4
gpt4 key购买 nike

我在渲染新 Controller 方法的表单时遇到此错误:

# 的未定义方法“teacher_id”提取的源代码(第 #8 行附近):

 <table summary="Student form fields">
<tr>
<th>Teacher ID</th>
<td><%= f.number_field :teacher_id %></td> # this is line 8
</tr>
<tr>
<th>Parent ID</th>

这是 new.html.erb:

<h1>Add A Student</h1>

<%= form_for @student, :url => { :action => 'create'}, :html => { :multipart => true } do |f| %>
<table summary="Student form fields">
<tr>
<th>Teacher ID</th>
<td><%= f.number_field :teacher_id %></td>
</tr>
<tr>
<th>Parent ID</th>
<td><%= f.number_field :parent_id %></td>
</tr>
<tr>
<th>FirstName</th>
<td><%= f.text_field :firstname %></td>
</tr>
<tr>
<th>LastName</th>
<td><%= f.text_field :lastname %></td>
</tr>
<tr>
<th>Date of Birth</th>
<td><%= f.date_field :dob %></td>
</tr>
<tr>
<th>Email</th>
<td><%= f.email_field :email %></td>
</tr>
<tr>
<th>Cellphone</th>
<td><%= f.telephone_field :cellphone %></td>
</tr>
<tr>
<th>Username</th>
<td><%= f.text_field :username %></td>
</tr>
<tr>
<tr>
<th>Password</th>
<td><%= f.password_field :password %></td>
</tr>
<tr>
<th>Confirm Password</th>
<td><%= f.password_field :password_confirmation %></td>
</tr>
<tr>
<th>Address Street#</th>
<td><%= f.text_field :addr_streetno %></td>
<th>Apt #</th>
<td><%= f.number_field :addr_aptno %></td>
</tr>
<tr>
<th>City</th>
<td><%= f.text_field :addr_city %></td>
<th>State</th>
<td><%= f.text_field :addr_state %></td>
<th>Zip</th>
<td><%= f.number_field :addr_zip %></td>
</tr>
<tr>
<th>Photo</th>
<td><%= f.file_field :photo %></td>
</tr>
</table>

<%= f.submit 'Create Student' %>
<% end %>

<% if @student.errors.any? %>
<ul class="Signup_Errors">
<% for message_error in @student.errors.full_messages %>
<li>* <%= message_error %></li>
<% end %>
</ul>
<% end %>
</div>

'这是student.rb模型文件:

class Student < ActiveRecord::Base
#attr_accessor :teacherid, :firstname, :lastname, :dob, :isadult,
# :email, :cellphone, :username, :password, :addr_streetno,
# :addr_city, :addr_state, :addr_zip, :photo

#has_secure_password

belongs_to :parent
belongs_to :teacher

attr_accessor :email, :password, :password_confirmation

EMAIL_REGEX = /\A[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\z/i
validates :username, :presence => true, :uniqueness => true, :length => { :in => 3..20 }
validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX
validates_confirmation_of :password

scope :sorted, lambda { order("students.firstname ASC")}
end

这是学生表的结构:

create_table :students do |t|
t.references :parents
t.references :teachers, null: false

t.string :firstname, null: false
t.string :lastname, null: false
t.date :dob, null: false
t.boolean :isadult, null: false, default: false
t.string :email, null: false
t.string :cellphone
t.string :username, null: false
t.string :password_digest, null: false
t.string :addr_streetno
t.integer :addr_aptno
t.string :addr_city
t.string :addr_state
t.integer :addr_zip
t.binary :photo, :limit => 0.5.megabyte

t.timestamps
end

最佳答案

您的表定义中显示了 teachers,而它应该是 teacher,因为它是 belongs_to。它应该这样读:

t.references :parent
t.references :teacher, null: false

关于mysql - 在新 Controller 方法中使用 form_for 时未定义方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23718445/

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