我有一个房东模型,一个LandlordCompany模型和一个LandlordAddress模型。我需要将landlord_id保存到landlord_comapnies表中,并将landlord_company_id保存到landlord_address表中。我有hidden_fields试图完成我过去所做的事情,但是在这种情况下不起作用。我想念什么?任何帮助都欢迎。提前致谢!
房东
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 <-房东公司
<div class="field">
<%= f.hidden_field :landlord_id, :value => params[:landlord_id] %>
</div>
_form.html.erb <-房东地址
<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
房东_公司控制人
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
房东地址控制器
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
您需要添加该房东公司的has_many
房东地址。然后,当您创建一个新对象时,您需要遵循文档并执行类似的操作landlord.landlord_companies.new(lord_params)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句