2016-04-27 11 views
0

Ich habe 2 Modelle Eigentum und Eigentum Daten. mehr Startdatum und Enddatum für eine Immobilie in Eigentum speichern Daten Tabellen Tabellenfelder (property_id, start_date_end_date) mein Modell Tabellen ich braucheSo speichern Sie mehrere Daten in Ruby

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

`class PropertyDate < ActiveRecord::Base 
 
\t belongs_to :property 
 
end`
meine Steuerung

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 ])

Form Eigenschaft Form muss ich mehrere Termine auswählen und property_dates Tabelle speichern müssen

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

Wenn i Schreibformular ist in meiner Form nicht sichtbar. Warum passiert es so? Irgendein Fehler in meinem Code. Bitte helfen Sie.

Antwort

0

einfach ändern:

def new 
    @property= Property.new 
    @property.property_dates.build 
end 
Verwandte Themen