Я пытаюсь отобразить имя факультета, а не только идентификатор.Ruby on rails - undefined method in show.html.erb
Все остальное работает, кроме той части, где я пытаюсь отобразить имя факультета. Я получаю сообщение об ошибке «неопределенный метод».
Я знаю, что я делаю что-то неправильно, но я не могу понять это вообще, хотя я смотрел на это часами. Я совершенно новичок, и я очень благодарен за вашу помощь.
спасибо.
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
Ошибка не определена метод `faculty_id 'для nil: NilClass
Проводка фактической ошибки будет большой помощью –