【问题标题】:Rails has_one association setupRails has_one 关联设置
【发布时间】:2013-03-30 21:50:38
【问题描述】:

您好,我是 rails 新手,并且已经在这个 has_one 关联中转了几个小时。我有产品和皮肤,当我通过表单创建新产品时,我想使用选择框来选择与产品关联的皮肤。

在皮肤表的模板列中保存了haml文件的名称后,我想在/skin/templates目录中使用haml文件渲染产品。

我得到的当前错误是:

undefined method `template' for nil:NilClass

对于控制器中的这一行:

render "/skins/templates/#{@product.skin.template}"

但是,我也尝试过使用 skin_id 的其他各种配置,但无法通过此操作。

代码如下:

products_controller.rb

class ProductsController < ApplicationController
  def show
    @product = Product.find(params[:id])

    if request.path != product_path(@product)
      redirect_to @product, status: :moved_permanently
    else 
      render "/skins/templates/#{@product.skin.template}"
    end
  end

  def new
    @product = Product.new

    respond_to do |format|
      format.html # new.html.haml
      format.json { render json: @product }
    end
  end

  def create
    @product = Product.new(params[:product])

    respond_to do |format|
      if @product.save
        format.html { redirect_to @product, notice: 'Product was successfully created.' }
        format.json { render json: @product, status: :created, location: @product }
      else
        format.html { render action: "new" }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end
end

product.rb

class Product < ActiveRecord::Base
  attr_accessible :name, :skin
  has_one :skin
end

皮肤.rb

class Skin < ActiveRecord::Base
  attr_accessible :product, :name, :template
  belongs_to :product
end

_form.html.haml

= form_for @product do |f|

  - if @product.errors.any?
    #error_explanation
      %h1= "#{pluralize(@product.errors.count, "error")} prohibited this product from being saved:"
      %ul
        - @product.errors.full_messages.each do |msg|
          %li= msg

  .field
    = f.label :name
    = f.text_field :name

  = f.select :skin_id, Skin.all.collect{|t| [t.name, t.id]}

  .actions
    = f.submit 'Save'

产品表

id  |     name     |         created_at         |         updated_at         
----+--------------+--------------------------------------------------------
  1 | test         | 2013-03-30 18:01:42.102505 | 2013-03-30 18:01:42.102505

皮肤表

 id |  name   | template |         created_at         |         updated_at         | product_id  
----+---------+----------+----------------------------+----------------------------+------------
  1 | Product | product  | 2013-03-30 20:13:26.374145 | 2013-03-30 20:13:26.374145 |             

【问题讨论】:

  • 您正在尝试对 nil 对象使用方法。如果你想避免这种情况,只需使用 @product.try(:skin).try(:template)

标签: ruby-on-rails associations belongs-to has-one


【解决方案1】:

皮肤记录中的product_id 为空...但看起来您的产品应该“属于”皮肤

1) 将 skin_id 添加到您的产品表中并从皮肤表中删除 product_id 2) 改变产品型号

 class Product < ActiveRecord::Base
      attr_accessible :name, :sku, :skin_id
      belongs_to :skin
      validates_presence_of :skin  #add validation
    end

3) 皮肤模型

class Skin < ActiveRecord::Base
  attr_accessible  :name, :template
  has_many :products
end

【讨论】:

  • 谢谢!这很好用。我唯一的问题是,为什么一个皮肤会有很多产品,而不是一个产品只有一个皮肤?
  • @greetification 产品属于皮肤,因为它具有对皮肤 (skin_id) 的外键引用。根据您的描述,似乎可以有很多产品使用任何一种皮肤,而这就是很多产品的来源。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多