我不知道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)
manuals / _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
选项Room
和Manual
具有HABTM关联,即M-M关系。
正如@MaxWilliams在他的答案中指出的那样,Rails期望room_ids
哈希中的params
数组而不是单个值。
为此,您应该允许用户通过在rooms
中的multiple: true
方法中添加html
作为grouped_collection_select
选项的形式(记住其M-M关系)来选择多个form
。而且,第一个参数应为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.从
room_id
表中删除manuals
根据问题中的共享日志
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]]
我看到您已经在
room_id
表中添加了manuals
字段。现在,这不是!没有!您在
Manual
和Room
之间没有1-M关联或1-1关联,但是它们之间的M-M关联与联接表manuals_rooms
已经包含room_id
和manual_id
字段。底线是您需要从
room_id
表中删除manuals
。生成迁移以删除
room_id
为:rails generate migration RemoveRoomIdFromManuals room_id:integer
运行
rake db:migrate
迁移更改。这应该解决您的问题。让我知道。
我真的开始认为这可能是一个预言
没有!
oracle_enhanced adapter
没有任何问题。导致此问题的是您的代码。关于ruby-on-rails - 在oracle_enhanced适配器上具有has_and_belongs_to_many,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25405516/