【问题标题】:Ruby on rails - undefined method in show.html.erbRuby on rails - show.html.erb 中未定义的方法
【发布时间】:2016-02-17 12:23:36
【问题描述】:

我正在尝试显示教职员工的姓名,而不仅仅是 ID。

除了我尝试显示教员姓名的那部分外,其他一切都有效。我收到一条错误消息“未定义的方法”。

我知道我做错了什么,但我根本无法弄清楚,即使我已经看了好几个小时。我是一个完全的初学者,非常感谢您的帮助。

谢谢。

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 %>

学生.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'

【问题讨论】:

  • 发布实际错误会很有帮助

标签: ruby-on-rails methods undefined


【解决方案1】:

我正在尝试显示教职员工的姓名,而不仅仅是 ID

既然你是初学者,那我给你解释一下……

--

您目前正在拨打@student.faculty_id

这是@student 对象的foreign_key -- 将这个student 对象链接到适当的faculty 对象的标识符。

简而言之,这意味着该属性是student 架构的一部分——您需要一个属于faculty 架构的属性。因此,您要么需要使用delegatefaculty 调用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 之上工作的。这允许您通过它们的外键调用相关对象。

如果需要,我可以解释更多。

【讨论】:

  • 非常感谢。这非常有帮助!
【解决方案2】:

&lt;%= @name.faculty_id %&gt; 不起作用。

在您的控制器中急切加载教师

def show
  @student = Student.includes(:faculty)
end

随便

<% if @student.faculty.present? %>
  <%= @student.faculty.name %>
<% end %>

或者您可以在控制器中获取教师并将其分配给变量

def show
  @student = Student.includes(:faculty)
  @faculty = @student.faculty
end

那么你就可以使用它了

<% if @faculty.present? %>
  <%= @faculty.name %>
<% end %>

【讨论】:

  • 感谢您的回复。我不明白你写的代码应该放在哪里。你能帮我吗?
  • 那么定义为 show 的位应该在控制器中,而另一位在您的视图中。
【解决方案3】:

我看到的错误是

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 不是单数形式

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-22
    • 1970-01-01
    • 2014-09-21
    • 1970-01-01
    • 2016-12-15
    相关资源
    最近更新 更多