Ruby 在 rails - show.html.erb 中未定义的方法
Ruby on rails - undefined method in show.html.erb
我正在尝试显示教师的姓名而不仅仅是 ID。
除我尝试显示教员姓名的那部分外,其他一切正常。我收到一条错误消息 "undefined method" 。
我知道我做错了什么,但我根本想不通,尽管我已经看了好几个小时了。我是一个完全的初学者,非常感谢您的帮助。
谢谢。
show.html.erb
<p id="notice"><%= notice %></p>
<!-- notice is a ruby method, and its results comes here inside the tags
used when you want the errow page to show on the next page -->
<p>
<strong>Name:</strong>
<%= @student.name %>
</p>
<p>
<strong>Faculty:</strong>
<%= @student.faculty_id %>
<%= @name.faculty_id %>
</p>
<strong>Grade:</strong>
<%= @student.grade%>
</p>
<%= link_to 'Edit', edit_student_path(@student) %> |
<%= link_to 'Back', students_path %>
student.rb
class Student < ActiveRecord::Base
belongs_to :faculty
end
class Name < ActiveRecord::Base
belongs_to :faculty
end
faculty.rb
class Faculty < ActiveRecord::Base
has_many :student
# belongs_to :faculty
has_many :name
end
这是我的 students_controler.rb
class StudentsController < ApplicationController
before_action :set_student, only: [:show, :edit, :update, :destroy]
# GET /students
# GET /students.json
def index
@students = Student.all
end
# GET /students/1
# GET /students/1.json
def show
end
# GET /students/new
def new
@student = Student.new
end
# GET /students/1/edit
def edit
end
# POST /students
# POST /students.json
def create
@student = Student.new(student_params)
respond_to do |format|
if @student.save
format.html { redirect_to @student, notice: 'Student was successfully created.' }
format.json { render :show, status: :created, location: @student }
else
format.html { render :new }
format.json { render json: @student.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /students/1
# PATCH/PUT /students/1.json
def update
respond_to do |format|
if @student.update(student_params)
format.html { redirect_to @student, notice: 'Student was successfully updated.' }
format.json { render :show, status: :ok, location: @student }
else
format.html { render :edit }
format.json { render json: @student.errors, status: :unprocessable_entity }
end
end
end
# DELETE /students/1
# DELETE /students/1.json
def destroy
@student.destroy
respond_to do |format|
format.html { redirect_to students_url, notice: 'Student was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_student
@student = Student.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def student_params
params.require(:student).permit(:name, :faculty_id)
end
end
错误是 nil:NilClass
的未定义方法 `faculty_id'
<%= @name.faculty_id %>
不行。
在你的控制器中预先加载教员
def show
@student = Student.includes(:faculty)
end
要么
<% if @student.faculty.present? %>
<%= @student.faculty.name %>
<% end %>
或者您可以获取控制器中的 faculty 并将其分配给变量
def show
@student = Student.includes(:faculty)
@faculty = @student.faculty
end
那你就可以用那个了
<% if @faculty.present? %>
<%= @faculty.name %>
<% end %>
我看到的错误是
class Faculty < ActiveRecord::Base
has_many :student
# belongs_to :faculty
has_many :name
end
应该是
class Faculty < ActiveRecord::Base
has_many :students
# belongs_to :faculty
has_many :names
end
我不知道你的错误是不是这样,但 has_many 不是单数形式
I am trying to display the name of the faculty instead of just the ID
既然你是初学者,那我就给你解释一下吧...
--
您正在呼叫 @student.faculty_id
这是 @student
对象的 foreign_key
-- 将此 student
对象链接到适当的 faculty
对象的标识符。
简而言之,这意味着此属性是 student
架构的一部分——您需要一个属于 faculty
架构的一部分。因此,您要么需要使用 delegate
从 faculty
调用 name
属性,要么直接调用它:
@student.faculty.name
您的模型关联存在更深层次的问题。
The above 应该如何设置:
#app/models/student.rb
class Student < ActiveRecord::Base
belongs_to :faculty
end
#app/models/faculty.rb
class Faculty < ActiveRecord::Base
has_many :students
end
以上将允许您调用以下内容:
#app/controllers/students_controller.rb
class StudentsController < ApplicationController
def show
@student = Student.find params[:id]
end
end
#app/views/students/view.html.erb
<%= @student.faculty.name %>
您必须记住 Rails 在 relational database 之上工作。这是通过允许您通过 外键 .
调用相关对象来实现的
如果需要,我可以解释更多。
我正在尝试显示教师的姓名而不仅仅是 ID。
除我尝试显示教员姓名的那部分外,其他一切正常。我收到一条错误消息 "undefined method" 。
我知道我做错了什么,但我根本想不通,尽管我已经看了好几个小时了。我是一个完全的初学者,非常感谢您的帮助。
谢谢。
show.html.erb
<p id="notice"><%= notice %></p>
<!-- notice is a ruby method, and its results comes here inside the tags
used when you want the errow page to show on the next page -->
<p>
<strong>Name:</strong>
<%= @student.name %>
</p>
<p>
<strong>Faculty:</strong>
<%= @student.faculty_id %>
<%= @name.faculty_id %>
</p>
<strong>Grade:</strong>
<%= @student.grade%>
</p>
<%= link_to 'Edit', edit_student_path(@student) %> |
<%= link_to 'Back', students_path %>
student.rb
class Student < ActiveRecord::Base
belongs_to :faculty
end
class Name < ActiveRecord::Base
belongs_to :faculty
end
faculty.rb
class Faculty < ActiveRecord::Base
has_many :student
# belongs_to :faculty
has_many :name
end
这是我的 students_controler.rb
class StudentsController < ApplicationController
before_action :set_student, only: [:show, :edit, :update, :destroy]
# GET /students
# GET /students.json
def index
@students = Student.all
end
# GET /students/1
# GET /students/1.json
def show
end
# GET /students/new
def new
@student = Student.new
end
# GET /students/1/edit
def edit
end
# POST /students
# POST /students.json
def create
@student = Student.new(student_params)
respond_to do |format|
if @student.save
format.html { redirect_to @student, notice: 'Student was successfully created.' }
format.json { render :show, status: :created, location: @student }
else
format.html { render :new }
format.json { render json: @student.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /students/1
# PATCH/PUT /students/1.json
def update
respond_to do |format|
if @student.update(student_params)
format.html { redirect_to @student, notice: 'Student was successfully updated.' }
format.json { render :show, status: :ok, location: @student }
else
format.html { render :edit }
format.json { render json: @student.errors, status: :unprocessable_entity }
end
end
end
# DELETE /students/1
# DELETE /students/1.json
def destroy
@student.destroy
respond_to do |format|
format.html { redirect_to students_url, notice: 'Student was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_student
@student = Student.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def student_params
params.require(:student).permit(:name, :faculty_id)
end
end
错误是 nil:NilClass
的未定义方法 `faculty_id'<%= @name.faculty_id %>
不行。
在你的控制器中预先加载教员
def show
@student = Student.includes(:faculty)
end
要么
<% if @student.faculty.present? %>
<%= @student.faculty.name %>
<% end %>
或者您可以获取控制器中的 faculty 并将其分配给变量
def show
@student = Student.includes(:faculty)
@faculty = @student.faculty
end
那你就可以用那个了
<% if @faculty.present? %>
<%= @faculty.name %>
<% end %>
我看到的错误是
class Faculty < ActiveRecord::Base
has_many :student
# belongs_to :faculty
has_many :name
end
应该是
class Faculty < ActiveRecord::Base
has_many :students
# belongs_to :faculty
has_many :names
end
我不知道你的错误是不是这样,但 has_many 不是单数形式
I am trying to display the name of the faculty instead of just the ID
既然你是初学者,那我就给你解释一下吧...
--
您正在呼叫 @student.faculty_id
这是 @student
对象的 foreign_key
-- 将此 student
对象链接到适当的 faculty
对象的标识符。
简而言之,这意味着此属性是 student
架构的一部分——您需要一个属于 faculty
架构的一部分。因此,您要么需要使用 delegate
从 faculty
调用 name
属性,要么直接调用它:
@student.faculty.name
您的模型关联存在更深层次的问题。
The above 应该如何设置:
#app/models/student.rb
class Student < ActiveRecord::Base
belongs_to :faculty
end
#app/models/faculty.rb
class Faculty < ActiveRecord::Base
has_many :students
end
以上将允许您调用以下内容:
#app/controllers/students_controller.rb
class StudentsController < ApplicationController
def show
@student = Student.find params[:id]
end
end
#app/views/students/view.html.erb
<%= @student.faculty.name %>
您必须记住 Rails 在 relational database 之上工作。这是通过允许您通过 外键 .
调用相关对象来实现的如果需要,我可以解释更多。