2013-07-21 109 views
0

一直困住這一段時間,我正在使用嚮導gem並希望以嵌套形式保存租戶屬性。屬性正在保存,但在數據庫中輸出nil。似乎不明白爲什麼不確定它的寶石,或者我缺少在模型或控制器中顯而易見的東西。在數據庫中顯示'nil'的嵌套屬性

參數

Parameters: {"utf8"=>"✓", "authenticity_token"=>"xPqiDsUpnuLHCSnU+XuAUce4b/cTnM/gv6T7wxdIz4g=", "property"=>{"tenants_attributes"=>{"0"=>{"title"=>"Mr", "firstname"=>"Foo", "surname"=>"bar", "dateofbirth(1i)"=>"2013", "dateofbirth(2i)"=>"7", "dateofbirth(3i)"=>"21", "telno"=>"01143268375", "contact_type"=>"foo", "email"=>"[email protected]"}}}, "commit"=>"Create Tenant", "property_id"=>"58"} 

這裏是零展示租戶日誌屬性。

INSERT INTO "tenants" ("contact_type", "created_at", "dateofbirth", "email", "firstname", "property_id", "surname", "telno", "title", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [["contact_type", nil], ["created_at", Sun, 21 Jul 2013 10:53:00 UTC +00:00], ["dateofbirth", nil], ["email", nil], ["firstname", nil], ["property_id", nil], ["surname", nil], ["telno", nil], ["title", nil], ["updated_at", Sun, 21 Jul 2013 10:53:00 UTC +00:00]] 

屬性/建立控制器

class Properties::BuildController < ApplicationController 


    include Wicked::Wizard 
    steps :tenant 

    def show 
     @property = Property.find(params[:property_id]) 
     @tenant = @property.tenants.new 
     render_wizard 
    end 

    def update 
    @property = Property.find(params[:property_id]) 
     @tenants = Tenant.find(params[:tenant]) 
     case step 
     when :tenant 
     if @tenants.update_attributes(params[:tenants]) 
     render_wizard @tenant 
     else 
     render :action => 'edit' 
     end 
    end 
end 

def create 
    @tenant = Tenant.create 
    if @tenant.save 
     flash[:success] = "Tenant Added" 
     redirect_to wizard_path(steps.first, :tenant_id => @tenant.id) 
    else 
     render 'edit' 
    end 
end 

屬性模型

class Property < ActiveRecord::Base 
    attr_accessible :name, :address_attributes, :tenants_attributes 
    belongs_to :user 

    has_one :address, :as => :addressable 
    accepts_nested_attributes_for :address 
    validates_associated :address 

    has_many :tenants 
    accepts_nested_attributes_for :tenants 



    validates :name, presence: true, length: { maximum: 200 } 
    validates :address, presence: true 
    validates :user_id, presence: true 

end 

租戶形式

<h2> Tenant Form</h2> 


<%= simple_form_for @property, :url => url_for(:action => 'create', :controller => 'properties/build'), :method => 'post' do |f| %> 
<%= f.simple_fields_for :tenants do |f| %> 
    <%= f.input :title %> 
    <%= f.input :firstname %> 
    <%= f.input :surname %> 
    <%= f.input :dateofbirth %> 
    <%= f.input :telno %> 
    <%= f.input :contact_type %> 
    <%= f.input :email %> 
    <%= f.submit %> 
<% end %> 

回答

0

您在視圖中有幾個錯誤。首先,你錯過了一個結束語句。其次,你使用f作爲外部和內部形式,所以你用內部重寫外部。嘗試是這樣的:

<%= simple_form_for @property, :url => url_for(:action => 'create', :controller => 'properties/build'), :method => 'post' do |f| %> 
    # Fields for property (f.input...) 
    <%= f.simple_fields_for :tenants do |tenant| %> 
    # Fields For tenant (tenant.input...) 
    <% end %> 
<% end %> 

另外,我覺得你這裏有一個錯誤

@tenant = Tenant.create 
if @tenant.save 
    flash[:success] = "Tenant Added" 
    redirect_to wizard_path(steps.first, :tenant_id => @tenant.id) 
else 
    render 'edit' 
end 

使用if something.save,你必須之前使用something.new代替something.createcreate創造了新的記錄,並保存到數據庫,不需要再次保存,new只是創建記錄,但必須手動保存)。

你可能需要像

@property = Property.new(params[:property]) 
if @property.save 
    flash[:success] = "Tenant Added" 
    redirect_to wizard_path(steps.first, :tenant_id => @tenant.id) 
else 
    render 'edit' 
end 
+0

謝謝你的提示做出那些改變。 – cyclopse87

+0

現在有效嗎? –

+0

不幸的是,仍然沒有相同的問題。 – cyclopse87