如何在 Ruby 中保存多个日期

问题描述 投票:0回答:1

我有 2 个型号的房产和房产日期。我需要在属性日期表中保存属性的多个开始日期和结束日期 表字段(property_id、start_date_end_date)。

我的模型表

`class Property < ActiveRecord::Base
    has_many :property_dates
    accepts_nested_attributes_for :property_dates
end`

class PropertyDate < ActiveRecord::Base
    belongs_to :property
end
我的控制器

class Users::PropertiesController < ApplicationController
  before_filter :authenticate_user!
  before_action :set_properties, only: [:show, :edit, :update, :destroy]


  def index
    @properties =  Property.where(:user_id=>current_user.id)
  end

  def list
    @properties = Property.all
  end

  def show

  end

 
  def new
   @property= Property.new
  end

  
  def edit
  end

  def create
    @property = Property.new(properties_params)
    respond_to do |format|
      if @property.save
 format.json { render :index, status: :created, location: @property }
      else
        format.html { render :new }
        format.json { render json: @property.errors, status: :unprocessable_entity }
      end
    end
  end

 
  def update
    respond_to do |format|
      if @property.update(properties_params)
        format.json { render :back, status: :ok, location: @property }
      else
        format.json { render json: @property.errors, status: :unprocessable_entity }
      end
    end
  end

 
  def destroy
    @property.destroy
    respond_to do |format|
      format.html { redirect_to  :back, notice: 'Property was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_properties
      @property = Property.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def properties_params
      params.require(:property).permit(:Space_name,:user_id,:address,:pincode,:image,property_dates_attributes: [ :start_date, :end_date ])

从属性表单中,我需要选择多个日期并需要保存到 property_dates 表

我的form.html.erb

 <%= simple_nested_form_for ([:users,@property])  do |f| %>
  <%= f.fields_for :property_dates do |p| %>
    <%= p.text_field :start_date%>
    <%= p.text_field :end_date%>
  <% end %>
  <% end %>

当我编写表单时,它在我的表单中不可见。为什么会出现这样的情况呢?我的代码有错误吗?

ruby-on-rails ruby-on-rails-3
1个回答
0
投票

改变它:

def new
  @property= Property.new
  @property.property_dates.build
end
© www.soinside.com 2019 - 2024. All rights reserved.