Rails在视图中显示查找表中的值



新手提醒!我希望我能准确地、可以理解地解释我的处境。

我有一个模型叫动物。另一个模型叫做状态。另一种叫做性别。

来自animal.rb模型:

class Animal < ActiveRecord::Base
    belongs_to :status, :class_name => Status, :foreign_key => :status_id
    belongs_to :sex, :class_name => Sex, :foreign_key => :sex_id
end

sex.rb模型:

class Sex < ActiveRecord::Base
    has_many :animals
end

status.rb模型:

class Status < ActiveRecord::Base
    has_many :animals
end

在我的观点/动物/index.html.erb我有这个:

<% @animals.each do |animal| %>
      <tr>
        <td><%= animal.name %></td>
        <td><%= animal.eartag %></td>
        <td><%= animal.sex.sex %></td>
        <td><%= animal.status.status %></td>
...

表中的列显示了性别查找表中的性别,但状态行会导致以下错误:

动物#索引中的NoMethodErrornil:NilClass 的未定义方法"status"

所以我猜Rails认为animal.status为零??我不知道为什么动物性行为不是零。

此外,我还有views/animals/show.html.erb。其中一部分是:

  <dt><strong><%= model_class.human_attribute_name(:sex_id) %>:</strong></dt>
  <dd><%= @animal.sex.sex %></dd>
 <dt><strong><%= model_class.human_attribute_name(:status_id) %>:</strong></dt>
  <dd><%= @animal.status.status %></dd>

这很好用。也就是说,在"性别"one_answers"状态"列中,它分别显示性别表和状态表的性别和状态列中的值,正如我希望index.html.erb视图所做的那样。

我不知道这是否有什么不同,但这里有animals_controller.rb:

class AnimalsController < ApplicationController
  before_action :set_animal, only: [:show, :edit, :update, :destroy]
  # GET /animals
  # GET /animals.json
  def index
    @animals = Animal.all
  end
  # GET /animals/1
  # GET /animals/1.json
  def show
  end
  # GET /animals/new
  def new
    @animal = Animal.new
  end
  # GET /animals/1/edit
  def edit
  end
  # POST /animals
  # POST /animals.json
  def create
    @animal = Animal.new(animal_params)
    respond_to do |format|
      if @animal.save
        format.html { redirect_to @animal, notice: 'Animal was successfully created.' }
        format.json { render action: 'show', status: :created, location: @animal }
      else
        format.html { render action: 'new' }
        format.json { render json: @animal.errors, status: :unprocessable_entity }
      end
    end
  end
  # PATCH/PUT /animals/1
  # PATCH/PUT /animals/1.json
  def update
    respond_to do |format|
      if @animal.update(animal_params)
        format.html { redirect_to @animal, notice: 'Animal was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @animal.errors, status: :unprocessable_entity }
      end
    end
  end
  # DELETE /animals/1
  # DELETE /animals/1.json
  def destroy
    @animal.destroy
    respond_to do |format|
      format.html { redirect_to animals_url }
      format.json { head :no_content }
    end
  end
  private
    # Use callbacks to share common setup or constraints between actions.
    def set_animal
      @animal = Animal.find(params[:id])
    end
    # Never trust parameters from the scary internet, only allow the white list through.
    def animal_params
      params.require(:animal).permit(:name, :eartag, :reg_num, :sex_id, :date_birth, :date_acquired, :date_removed, :status_id, :num_horns, :sire_id, :dam_id, :source_id, :origin_id, :percent_black, :percent_lilac, :for_sale, :for_sale_status_id, :quality_id, :sale_price, :to_be_culled, :comments, :comments_web, :show_on_website, :core_flock, :birth_id, :rejected_at_birth)
    end
end

我希望这有点道理。谢谢

更改此行

<%= animal.status.status %>

<%= animal.status.status if animal.status %>

这将防止出现错误。仅当存在animal.status时才会输出animal.status.status

最新更新