【问题标题】:Nested attributes showing 'nil' in database在数据库中显示“nil”的嵌套属性
【发布时间】:2013-07-21 11:34:26
【问题描述】:

在此问题上停留了一段时间,我正在使用向导 gem 并希望以嵌套形式保存租户属性。属性正在保存,但在数据库中输出 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"=>"example@gmail.com"}}}, "commit"=>"Create Tenant", "property_id"=>"58"}

这里是显示租户属性为 nil 的日志。

 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 %>

【问题讨论】:

    标签: ruby-on-rails ruby simple-form wizard formwizard


    【解决方案1】:

    您的视图中有几个错误。首先,您缺少一个结束语句。其次,您将 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
    

    【讨论】:

    • 感谢提示做出这些更改。
    • 很遗憾没有遇到同样的问题。
    • 你能发布你的简单表单生成的html吗?
    • 它很长,所以我把它贴在馅饼上,请看这里。 pastie.org/8160970
    • 您还必须有姓名、地​​址和用户 ID,因为您正在验证它们是否存在(如果存在为真,则它们是强制性的)。
    猜你喜欢
    • 2014-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多