2013-03-30 65 views
1

您好我是Rails新手,並且已經與這個has_one關聯圈了幾個小時。我有產品和皮膚,當我通過表單創建新產品時,我想使用選擇框來選擇與產品關聯的外觀。Rails has_one關聯設置

我想在保存hains文件的名稱到skins表格的模板列後,在/ skin/templates目錄下用haml文件渲染產品。

的當前錯誤,我得到的是:

undefined method `template' for nil:NilClass 

此行控制器:使用skin_id

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

不過,我已經試過其它各種配置以及和沒有能夠克服這一點。

下面的代碼:

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 

skin.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 |    
+0

您嘗試零對象使用方法。如果你想避免這種情況,只需使用@ product.try(:skin).try(:template) –

回答

0

的product_id皮膚紀錄是空的......不過貌似你的產品應該 「belongs_to的」 皮膚

1)添加skin_id到您的產品表並從skin表中刪除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 
+0

謝謝!這很好。我唯一的問題是,爲什麼皮膚會有很多產品,而不是產品只有一個皮膚? –

+1

@greetification產品屬於皮膚,因爲它具有對皮膚的外鍵引用(skin_id)。從你的描述來看,似乎可以有許多產品使用任何單一的皮膚,而這正是許多產品的來源。 – graywh