У меня есть модель арендодателя и landlord_address. Когда создается новый landlord_address, landlord_id сохраняется в таблице. По какой-то причине, когда я редактирую landlord_address, landlord_id изменяется на NULL.ID изменен на NULL при обновлении - Rails 4
Модели:
class Landlord < ActiveRecord::Base
has_many :landlord_addresses, dependent: :destroy
belongs_to :listing_agent, class_name: 'Agent'
end
class LandlordAddress < ActiveRecord::Base
belongs_to :landlord
has_many :landlord_companies, dependent: :destroy
end
Арендодатель Адреса Контроллер:
module Matrix
class LandlordAddressesController < ApplicationController
before_action :set_landlord_address, only: [:show, :edit, :update, :destroy]
# GET /landlord_addresses
# GET /landlord_addresses.json
def index
@landlord = Landlord.find(params[:landlord_id])
@landlord_addresses = @landlord.landlord_addresses.order(address_line_one: :asc)
end
# GET /landlord_addresses/1
# GET /landlord_addresses/1.json
def show
end
# GET /landlord_addresses/new
def new
@landlord_address = LandlordAddress.new
@landlord = Landlord.find(params[:landlord_id])
end
# GET /landlord_addresses/1/edit
def edit
end
# POST /landlord_addresses
# POST /landlord_addresses.json
def create
@landlord_address = LandlordAddress.new(landlord_address_params)
respond_to do |format|
if @landlord_address.save
format.html { redirect_to matrix_landlord_landlord_addresses_path, notice: 'Landlord address was successfully created.' }
format.json { render :show, status: :created, location: @landlord_address }
else
format.html { render :new }
format.json { render json: @landlord_address.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /landlord_addresses/1
# PATCH/PUT /landlord_addresses/1.json
def update
respond_to do |format|
if @landlord_address.update(landlord_address_params)
format.html { redirect_to matrix_landlord_landlord_addresses_path, notice: 'Landlord address was successfully updated.' }
format.json { render :show, status: :ok, location: @landlord_address }
else
format.html { render :edit }
format.json { render json: @landlord_address.errors, status: :unprocessable_entity }
end
end
end
# DELETE /landlord_addresses/1
# DELETE /landlord_addresses/1.json
def destroy
@landlord_address.destroy
respond_to do |format|
format.html { redirect_to landlord_addresses_url, notice: 'Landlord address was successfully destroyed.' }
format.json { head :no_content }
end
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_id, :latitude, :longitude)
end
end
end
арендодателей Адреса _form:
<div class="feedback-container">
<%= form_for [:matrix, @landlord_address] do |f| %>
<% if @landlord_address.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@landlord_address.errors.count, "error") %> prohibited this landlord_address from being saved:</h2>
<ul>
<% @landlord_address.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
/ul>
</div>
<% end %>
<div id="form-map"></div>
<input id="pac-input" class="controls" type="text"
placeholder="Enter a location">
<div id="type-selector" class="controls">
<input type="radio" name="type" id="changetype-all" checked="checked">
<label for="changetype-all">All</label>
</div>
<div class="field">
<%= f.label :address, class: "general-text-label" %>
<%= f.text_field :address_line_one, class: "general-text-field map-autocomplete-address" %>
<%= f.hidden_field :latitude, class: "map-autocomplete-latitude" %>
<%= f.hidden_field :longitude, class: "map-autocomplete-longitude" %>
</div>
<div class="field">
<%= f.label :super_name, class: "general-text-label" %><br>
<%= f.text_field :super_name, class: "general-text-field" %>
</div>
<div class="field">
<%= f.label :super_number, class: "general-text-label" %><br>
<%= f.text_field :super_number, class: "general-text-field" %>
</div>
<div class="field">
<%= f.hidden_field :landlord_id, :value => params[:landlord_id] %>
</div><br>
<div class="actions">
<%= f.submit "Submit", class: "btn btn-black btn-4x" %>
</div>
<% end %>
</div>
Маршруты:
namespace :matrix do
resources :landlords, shallow: true do
resources :landlord_addresses do
resources :landlord_companies
end
end
end
Новая ошибка:
тест, что я писал, дайте мне знать, я – 7urkm3n
заметить, там нет Титулы [: landlord_id] попробовать это, он должен работать. 'format.html {redirect_to" /matrix/landlords/#{@landlord_address.landlord_id}/landlord_addresses ", обратите внимание:« Адрес арендодателя был успешно обновлен ». } ' – 7urkm3n