[英]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 %>
我在這里看到幾個問題。 讓我一一強調它們:
multiple: true
選項 Room
和Manual
具有HABTM關聯,即MM關系 。
正如@MaxWilliams在回答中指出的那樣,Rails希望在params
哈希中room_ids
一個room_ids
數組,而不是單個值。
為此,您應該允許用戶通過在表單中的grouped_collection_select
方法中添加multiple: true
作為html
選項, grouped_collection_select
在form
選擇多個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} %>
room_ids
作為數組 由於上面的更改#1 ,由於room_ids
現在將作為數組傳遞到params
哈希中,因此應允許將它作為Array
。
def manual_params
params.require(:manual).permit(:name, :document, :room_ids => [])
end
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
字段。 現在,這不是! 沒有!
您在Manual
和Room
之間沒有1-M關聯或1-1關聯 ,但它們之間的MM關聯與manuals_rooms
表manuals_rooms
已經包含room_id
和manual_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.