Bru*_*uno 2 activerecord ruby-on-rails ruby-on-rails-4
我不知道这是否是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
Run Code Online (Sandbox Code Playgroud)
当我创建新手册时,它不会更新我的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
Run Code Online (Sandbox Code Playgroud)
日志
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)
Run Code Online (Sandbox Code Playgroud)
我改变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)
Run Code Online (Sandbox Code Playgroud)
手册/ _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 %>
Run Code Online (Sandbox Code Playgroud)
我在这看到几个问题.让我一个一个地突出它们:
multiple: true
选项Room
并且Manual
有一个HABTM关联,即MM关系.
作为@MaxWilliams在他的回答中指出,Rails的期望的数组room_ids
中params
的哈希,而不是一个单一的值.
要做到这一点,你应该允许用户选择多个rooms
形式(请记住,它的MM关系加入)multiple: true
作为一个html
选项,grouped_collection_select
在你的方法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} %>
Run Code Online (Sandbox Code Playgroud)
room_ids
作为数组由于上面的变更#1,room_ids
现在将作为params
散列中的数组传递,因此应该允许它作为.Array
def manual_params
params.require(:manual).permit(:name, :document, :room_ids => [])
end
Run Code Online (Sandbox Code Playgroud)
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]]
Run Code Online (Sandbox Code Playgroud)
我看到你已经添加了room_id
字段到manuals
表.现在这是一个不!没有!
你没有1-M协会或1-1协会之间Manual
和Room
,但有一个MM公会它们之间有一个连接表manuals_rooms
已经包含room_id
和manual_id
领域.
底线是您需要从表中删除 .room_id
manuals
生成迁移以进行删除room_id
:
rails generate migration RemoveRoomIdFromManuals room_id:integer
Run Code Online (Sandbox Code Playgroud)
运行rake db:migrate
以迁移更改.
这应该照顾你的问题.让我知道.
我真的开始认为这可能是一个神谕
没有!这没有什么不对oracle_enhanced adapter
.这是导致问题的代码.
归档时间: |
|
查看次数: |
129 次 |
最近记录: |