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

时间:2013-12-02 05:03:50

标签: ruby-on-rails associations

新手提醒!我希望我能准确,可理解地解释我的情况。

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

来自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

在我的观点/ animals / 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>
...

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

Animal#index中的NoMethodError nil的未定义方法`status':NilClass

所以我猜Rails认为animal.status是零?我不知道为什么当animal.sex不是零时会是这样。

另外,我有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>

这很好用。即在性别和状态列中,它分别显示性别和状态表的性别和状态列中的值,因为我想要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

我希望这有一定道理。谢谢!

1 个答案:

答案 0 :(得分:0)

更改此行

<%= animal.status.status %>

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

这样可以防止出错。仅在animal.status.status出现时输出animal.status