2016-09-23 81 views
0

我有一個Landlord模型,一個LandlordCompany模型和一個LandlordAddress模型。我需要將landlord_id保存到landlord_comapnies表,將landlord_company_id保存到landlord_address表。我有hidden_​​fields試圖完成我以前所做的這一點,但它在這種情況下不起作用。我錯過了什麼?任何幫助歡迎。提前致謝!ID不通過Rails中的模型/控制器保存到數據庫4

landlord.rb

class Landlord < ActiveRecord::Base 
    has_many :landlord_companies 
end 

landlord_company.rb

class LandlordCompany < ActiveRecord::Base 
    belongs_to :landlord 
end 

landlord_address.rb

class LandlordAddress < ActiveRecord::Base 
    belongs_to :landlord_company 
end 

_form.html.erb < - landlord_companies

<div class="field"> 
    <%= f.hidden_field :landlord_id, :value => params[:landlord_id] %> 
</div> 

_form.html.erb < - landlord_addresses

<div class="field"> 
    <%= f.hidden_field :landlord_company_id, :value => params[:landlord_company_id] %> 
</div> 

的routes.rb

resources :landlords do 
    member do 
     resources :landlord_companies do 
     member do 
      resources :landlord_addresses 
     end 
     end 
    end 
end 

地主控制器

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

# Never trust parameters from the scary internet, only allow the white list through. 
def landlord_params 
    params.require(:landlord).permit(:name, :rating, :contact_name, :contact_number, :listing_agent_id, :own_application, :own_credit, :accepts_roommate_matchups, :management_company) 
end 

Landlord_companies控制器

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

# Never trust parameters from the scary internet, only allow the white list through. 
def landlord_company_params 
    params.require(:landlord_company).permit(:llc_name, :landlord_id) 
end 

Landlord_addresses控制器

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

# Never trust parameters from the scary internet, only allow the white list through. 
def landlord_address_params 
    params.require(:landlord_address).permit(:address_line_one, :address_line_two, :city, :state, :zip, :super_name, :super_number, :landlord_company_id, :latitude, :longitude) 
end 
+0

你在控制器中使用強參數嗎?他們看起來像什麼? – spickermann

+0

您需要補充的是,房東公司有很多房東地址。然後,當你創建一個新的對象,你需要按照文檔,並做一些像landlord.landlord_companies.new(lord_params) – bkunzi01

+0

@spickermann我加了他們。 –

回答

1

您需要添加房東企業has_many房東的地址。然後,當你創建一個新對象時,你需要按照文檔做類似landlord.landlord_companies.new(lord_params)

相關問題