在Parent的视图中显示Child的值; has_one belongs_to association

时间:2011-12-01 01:12:43

标签: ruby-on-rails ruby-on-rails-3 associations rails-activerecord

我正在尝试在父视图文件中显示子项的列值。 该应用程序不会抛出任何错误,但由于某种原因它始终返回nil值,并且从不显示该值。

在我的应用中,Resume has_one Extra。

这是我的代码。

恢复视图(Show.html.erb)

<!--This line shows nothing. Returns nil all the time even though the cell is not empty -->
<% @resume.extra.interests %> 

协会

class Resume < ActiveRecord::Base
  has_one   :extra
  accepts_nested_attributes_for :extra
end

class Webconnection < ActiveRecord::Base
  belongs_to :resume
end

恢复控制器

class ResumesController < ApplicationController
def show
  @resume = Resume.find(params[:id])
end

def new
  @title = "Create a new resume"
  @resume = Resume.new
  experience = @resume.experiences.build
  education = @resume.educations.build
  extra = @resume.build_extra
  webconnection = @resume.build_webconnection
end

def create
  @resume = Resume.new(params[:resume])     
  if @resume.save
    #UserMailer.created_resume_email(@user).deliver
    redirect_to @resume
  else
    @title = "Create a new resume"
    render :action => "new"
  end
end
end

网络连接的迁移文件

class CreateExtras < ActiveRecord::Migration
  def change
    create_table  :extras do |t|
      t.text      :interests
      t.text      :recommendations
      t.text      :awards
      t.text      :activities
      t.integer   :resume_id

      t.timestamps
    end
  end
end

1 个答案:

答案 0 :(得分:2)

替换

<% @resume.extra.interests %>

通过

<%= @resume.extra.interests %>

<% foo %>评估foo但不打印任何东西。

<%= foo %>评估并打印foo