【问题标题】:ID changed to NULL on update - Rails 4ID 在更新时更改为 NULL - Rails 4
【发布时间】:2016-09-30 17:44:09
【问题描述】:

我有一个房东和房东地址模型。创建新的landlord_address 时,将landlord_id 保存到表中。出于某种原因,当我编辑一个地主地址时,地主 ID 被更改为 NULL。

型号:

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

新错误:

【问题讨论】:

  • 测试我发布的内容,让我知道
  • 我注意到,没有参数[:landlord_id] 试试这个应该可以的。 format.html { redirect_to "/matrix/landlords/#{@landlord_address.landlord_id}/landlord_addresses", notice: 'Landlord address was successfully updated.' }

标签: ruby-on-rails postgresql model-view-controller null


【解决方案1】:

这部分

 <%= f.hidden_field :landlord_id, :value => params[:landlord_id] %>

将是您的问题。当params[:landlord_id]nil 时,您的记录将更新为null

你应该把它改成

 <%= f.hidden_field :landlord_id, :value => @landlord_address.landlord_id || @landlord.id %> 

或者别的什么。

【讨论】:

  • 那行不通。 ID 最初是保存的,但更新后仍会变为 NULL。
  • 在您的日志文件中或通过调试器检查landlord_address_params。我认为出于某些原因landlord_id 为零。
  • 我添加了上面的错误,看来地主id没有通过。
【解决方案2】:

您的路线是嵌套的,您不需要在隐藏字段中分配。您可以在控制器中执行此操作,并传递参数。

表格:

移除隐藏字段,我们在控制器中分配它!

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

控制器:

# POST /landlord_addresses
# POST /landlord_addresses.json
def create
  @landlord_address = LandlordAddress.new(landlord_address_params)

  # add this line here!
  @landlord_address.landlord_id = params[:landlord_id]

  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


private

#ALSO REMOVE `:landlord_id` from 
def landlord_address_params
    params.require(:landlord_address).permit(:address_line_one, :address_line_two, :city, :state, :zip, :super_name, :super_number, :latitude, :longitude)
end

【讨论】:

  • 那行不通。 ID 仍在更新时更改为 NULL。知道为什么吗?
  • 对不起,我的错误可能应该是 params[:landlord_id],更新后的帖子。另外,不要更新它,创建新的房东地址。
  • 我更改了它,它仍然更改为 NULL。我添加了上面的错误,看起来地主id没有被传递。
  • 那行得通,现在我只是遇到了重定向问题,但 ID 仍然存在!谢谢。
  • @MikeWiesenhart,它只是缺少 DB 中的 landlord_id。您只需要在创建时分配一次就足够了。不要忘记更新控制器,再次查看我的帖子以从 .permit 中删除 :landlord_id...
猜你喜欢
  • 1970-01-01
  • 2013-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-23
  • 1970-01-01
  • 1970-01-01
  • 2019-03-07
相关资源
最近更新 更多