簡體   English   中英

在oracle_enhanced適配器上具有has_and_belongs_to_many

[英]has_and_belongs_to_many on oracle_enhanced adapter

我不知道oracle_enhanced適配器是否有問題。 我有:

class Room < ActiveRecord::Base
  has_and_belongs_to_many :manuals
end

class Manual < ActiveRecord::Base
  has_and_belongs_to_many :rooms
end

class CreateJoinTableManualRoom < ActiveRecord::Migration
  def change
    create_join_table :manuals, :rooms do |t|
      t.index [:manual_id, :room_id]
      t.index [:room_id, :manual_id]
    end
  end
end

創建新手冊時,它不會更新我的manuals_rooms連接表。

class ManualsController < ApplicationController
  before_action :set_manual, only: [:show, :edit, :update, :destroy]

  def index
    @manuals = Manual.all
    @rooms = Room.all
  end

  def show
  end

  def new
    @manual = Manual.new
  end

  def edit
  end

  def create
    @manual = Manual.new(manual_params)

    respond_to do |format|
      if @manual.save
        format.html { redirect_to @manual, notice: 'Manual was successfully created.' }
        format.json { render action: 'show', status: :created, location: @manual }
      else
        format.html { render action: 'new' }
        format.json { render json: @manual.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @manual.update(manual_params)
        format.html { redirect_to @manual, notice: 'Manual was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @manual.errors, status: :unprocessable_entity }
      end
    end
  end

  private
    def set_manual
      @manual = Manual.find(params[:id])
    end

    def manual_params
      params.require(:manual).permit(:name, :document, :room_ids => [])
    end
end

日志

Started POST "/manuals" for 127.0.0.1 at 2014-08-20 09:12:12 -0400
Processing by ManualsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"Ntqqh1vU9zrGZuw3K8xNef440ktAixWj+6Cx20wrCRg=", "manual"=>{"document"=>#<ActionDispatch::Http::UploadedFile:0x000001134643d0 @tempfile=#<Tempfile:/var/folders/d1/x0nbfyrj30bd_p33ds0f12_c0000gq/T/RackMultipart20140820-59361-1jkeynu>, @original_filename="103.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"manual[document]\"; filename=\"103.jpg\"\r\nContent-Type: image/jpeg\r\n">, "room_id"=>"3"}, "commit"=>"Upload"}
Unpermitted parameters: room_id
  SQL (1.8ms)  INSERT INTO "MANUALS" ("CREATED_AT", "DOCUMENT", "ID", "UPDATED_AT") VALUES (:a1, :a2, :a3, :a4)  [["created_at", Wed, 20 Aug 2014 09:12:12 EDT -04:00], ["document", "103.jpg"], ["id", 10021], ["updated_at", Wed, 20 Aug 2014 09:12:12 EDT -04:00]]
Redirected to http://localhost:3000/manuals/10021
Completed 302 Found in 16ms (ActiveRecord: 1.8ms)

我將room_ids => []更改為room_id ,現在我得到:

  SQL (2.5ms)  INSERT INTO "MANUALS" ("CREATED_AT", "DOCUMENT", "ID", "ROOM_ID", "UPDATED_AT") VALUES (:a1, :a2, :a3, :a4, :a5)  [["created_at", Wed, 20 Aug 2014 09:20:43 EDT -04:00], ["document", "AMH_BW.jpg"], ["id", 10022], ["room_id", 3], ["updated_at", Wed, 20 Aug 2014 09:20:43 EDT -04:00]]
Redirected to http://localhost:3000/manuals/10022
Completed 302 Found in 56ms (ActiveRecord: 19.0ms)

手冊/ _form.html.erb

<%= form_for(@manual) do |f| %>
  <div class="field">
    <%= f.label :document %><br>
    <%= f.file_field :document %>
  </div>
  <div class="field">
    <%#= collection_select(:manual, :room_ids, Room.all, :id, :name) %>
    <%= f.grouped_collection_select :room_id, Building.order(:name), :rooms, :name, :id, :name_with_number, include_blank: true %>                            
  </div>
  <div class="actions">
    <%= f.submit "Upload" %>
  </div>
<% end %>

我在這里看到幾個問題。 讓我一一強調它們:

1.缺少multiple: true選項

RoomManual具有HABTM關聯,即MM關系

正如@MaxWilliams在回答中指出的那樣,Rails希望在params哈希中room_ids一個room_ids數組,而不是單個值。

為此,您應該允許用戶通過在表單中​​的grouped_collection_select方法中添加multiple: true作為html選項, grouped_collection_selectform 選擇多個rooms (請記住,其MM關系 )。 而且,第一個參數應該是room_ids而不是room_id

<%= f.grouped_collection_select :room_ids, Building.order(:name), :rooms, :name, :id, :name_with_number, {include_blank: true}, {multiple: true} %>  

2.允許將room_ids作為數組

由於上面的更改#1 ,由於room_ids現在將作為數組傳遞到params哈希中,因此應允許將它作為Array

def manual_params
  params.require(:manual).permit(:name, :document, :room_ids => [])
end

3.從manuals表中刪除room_id

根據問題中的共享日志

SQL (2.5ms)  INSERT INTO "MANUALS" ("CREATED_AT", "DOCUMENT", "ID", "ROOM_ID", "UPDATED_AT") VALUES (:a1, :a2, :a3, :a4, :a5)  [["created_at", Wed, 20 Aug 2014 09:20:43 EDT -04:00], ["document", "AMH_BW.jpg"], ["id", 10022], ["room_id", 3], ["updated_at", Wed, 20 Aug 2014 09:20:43 EDT -04:00]]

我看到您已經在manuals表中添加了room_id字段。 現在,這不是! 沒有!

您在ManualRoom之間沒有1-M關聯1-1關聯 ,但它們之間的MM關聯manuals_roomsmanuals_rooms已經包含room_idmanual_id字段。

底線是您需要從manuals表中刪除 room_id

生成遷移以將room_id刪除為:

rails generate migration RemoveRoomIdFromManuals room_id:integer

運行rake db:migrate以遷移更改。

這應該解決您的問題。 讓我知道。


我真的開始認為這可能是一個預言

沒有! oracle_enhanced adapter沒有任何問題。 導致此問題的是您的代碼。

由於這是一個habtm關系,因此您希望獲得params[:room_ids]並將其作為一個數組。 表單中輸入的實際名稱應為room_ids[] -末尾的數組告訴rails收集名稱為room_ids[]所有params並將它們收集到params[:room_ids]的數組中。

因此,您希望在呈現的html中看到以下內容(例如,如果是復選框):

<input name="room_ids[]" type="checkbox" value="3" />

如果這不在您呈現的html中(請使用chrome inspector或類似工具查看該頁面),則說明您的表單設置不正確。 請在另一個編輯中將表單代碼添加到您的帖子中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM