2016-12-02 60 views
0

我有兩個型號:
1.Personne
無法保存在軌嵌套屬性5

class Personne < ApplicationRecord 
    has_one :proprietaire 

    accepts_nested_attributes_for :proprietaire 
    validates :nom, :prenom, :tel, :email, 
      presence: true 
end 

2的Propriétaire

class Proprietaire < ApplicationRecord 
    belongs_to :personne 

    validates :commune_id, :quartier, 
      presence: true 
end 

控制器是:

class PersonneController < ApplicationController 
    def display_proprietaires 
    @proprietaires = Personne.all 
    #@proprietaires = @proprietaires.proprietaire 
    end 

    def new_proprietaire 
    @provinces = Province.where(:parentId => nil) 
    @communes = Province.where.not(:parentId => nil) 
    @personne = Personne.new 
    @personne.build_proprietaire 
    end 

    def create_proprietaire 
    @proprietaire = Personne.new(proprietaire_params) 
    @proprietaire.build_proprietaire 
    respond_to do |format| 
     if @proprietaire.save 
     flash[:notice] = "succes" 
     flash[:type] = "success" 
     format.html { redirect_to action: :display_proprietaires } 
     else 
     flash[:notice] = "fail" 
     flash[:type] = "warning" 
     format.html { redirect_to action: :display_proprietaires } 
     end 
    end 
    end 

    def proprietaire_params 
    params.require(:personne).permit(:nom, :prenom, :tel, :email, proprietaire_attributes: [:id, :commune_id, :quartier]).except(:province, :commit) 
    end 
end 

的觀點是:

<%= form_for @personne, :url => url_for(:controller=>'personne', :action=>'create_proprietaire') do |f| %> 
    <div class="row"> 
    <div class="col-xs-6 col-sm-6 col-lg-6"> 
     <div class="form-group"> 
      <%= f.label(:nom, 'Nom : ') %> 
      <%= f.text_field :nom, {class: "form-control", placeholder: 'Nom'} %> 
     </div> 
     <div class="form-group"> 
      <%= f.label(:prenom, 'Prenom : ')%> 
      <%= f.text_field :prenom, {class: "form-control", placeholder: "Prenom"} %> 
     </div> 
     <div class="form-group"> 
      <%= f.label(:tel, 'Telephone : ')%> 
      <%= f.text_field :tel, {class: "form-control", placeholder: "Telephone"} %> 
     </div> 
     <div class="form-group"> 
      <%= f.label(:email, 'Email : ') %> 
      <%= f.text_field :email, {class: "form-control", placeholder: "Email"} %> 
     </div> 
     <div class="form-group"> 
      <%= label_tag(:province, 'Province : ') %> 
      <%= select_tag(:province, options_for_select(@provinces.collect{|value| [value.denomination, value.id]}), {class: "form-control", id: "province", remote: true}) %> 
     </div> 
     <%= f.fields_for :proprietaire do |proprio| %> 
      <div class="form-group"> 
       <%= proprio.label(:commune_id, 'Commune : ') %> 
       <%= proprio.select :commune_id, options_for_select(@communes.collect{|value| [value.denomination, value.id]}),{}, {class: "form-control", id: "commune"} %> 
      </div> 
      <div class="form-group"> 
       <%= proprio.label :quartier, "Quartier" %> 
       <%= proprio.text_field :quartier, {class: "form-control", placeholder: "Quartier"} %> 
      </div> 
     <% end %> 
     <%= f.submit "Enregistre", {class: 'btn btn-info'} %> 
<% end %> 

路線:

resources :personne do 
    collection do 
     post :create_proprietaire 
     get :display_proprietaires 
     get :new_proprietaire 
    end 
    end 

我在回報率是全新的,當我試圖挽救什麼也沒有發生,我得到這個: Error

有人可以幫助我解決這個問題。謝謝!

+0

你有你的驗證失敗,或者需要協會沒有設置 – AndreyS

+0

@AndreyS非常感謝你很多,這是沒有設定的必要協會。我在這裏找到了解決方案http://blog.bigbinary.com/2016/02/15/rails-5-makes-belong-to-association-required-by-default.html – hakif

+0

好吧,我會將它作爲答案發布所以 – AndreyS

回答

0

您的關聯設置爲必填項,但缺失。 協會被默認設置爲在軌道所需的5,所以如果你想保持一個空的,你需要設置可選:在模型真正對你聯想