我有用戶和配對型號爲這樣:不能創建從表單提交新的數據庫條目
class User < ActiveRecord::Base
enum role: [:student, :supervisor, :admin]
has_many :students, class_name: "User",
foreign_key: "supervisor_id"
belongs_to :supervisor, class_name: "User"
has_and_belongs_to_many :pairings
end
class Pairing < ActiveRecord::Base
has_and_belongs_to_many :supervisor, class_name: 'User'
belongs_to :student, class_name: 'User'
end
而且從架構相關的部分(大小去除色器件領域):
ActiveRecord::Schema.define(version: 20160221222318) do
create_table "pairings", force: :cascade do |t|
t.integer "supervisor_id"
t.integer "student_id"
t.string "project_title"
end
add_index "pairings", ["student_id"], name: "index_pairings_on_student_id", unique: true
add_index "pairings", ["supervisor_id"], name: "index_pairings_on_supervisor_id"
create_table "users", force: :cascade do |t|
t.string "name"
t.integer "role"
t.integer "supervisor_id"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
end
我有一個表格,其中有1個下拉框列出所有學生,1個列出所有主管。我的目標是雙重的:
(1)一旦從方塊中選擇了一位主管和學生,獲取他們的ID並將主管的ID分配給學生的supervisor_id列。 (使用AJAX修補程序請求完成)
(2)在配對錶中創建一個新行,並使用來自下拉列表的ID。 (通過pairings_controllers完成創建方法)
這是我的看法:
<script>
$("#my_special_form").submit(function(){
var url = "https://stackoverflow.com/users/" + this.elements["pairing[student_id]"].value;
$.ajax(url, {
beforeSend: $.rails.CSRFProtection,
data: {
user: {
supervisor_id: this.elements["pairing[supervisor_id]"].value
}
},
type: 'PATCH',
dataType: 'JSON',
success: function(data, textStatus, jqXHR) {
console.log('Success', data, textStatus, jqXHR);
},
});
return false; // cancels out the normal form submission
});
</script>
<%= form_for(Pairing.new, url: pairings_path, method: :post, html: { id: 'my_special_form' }) do |f| %>
<div class="field">
<%= f.label :supervisor_id %>
<%= f.collection_select(:supervisor_id, User.where(role: 1), :id, :name) %>
</div>
<div class="field">
<%= f.label :student_id %>
<%= f.collection_select(:student_id, User.where(role: 0), :id, :name) %>
</div>
<div class ="field">
<%= f.label :project_title %>
<%= f.text_field :project_title %>
<%= f.submit %>
<% end %>
和控制器:
class PairingsController < ApplicationController
def new
@pairing = Pairing.new
end
def create
@pairing = Pairing.new(pair_params)
end
def update
@pairing = Pairing.find(params[:id])
@pairing.update_attributes(pairing_params)
end
private
def pair_params
params.require(:pairing).permit(:supervisor_id, :student_id, :project_title)
end
end
class UsersController < ApplicationController
before_action :authenticate_user!
after_action :verify_authorized
def index
@users = User.all
authorize User
end
def show
@user = User.find(params[:id])
authorize @user
end
def update
@user = User.find(params[:id])
authorize @user
if @user.update_attributes(secure_params)
redirect_to users_path, :notice => "User updated."
else
redirect_to users_path, :alert => "Unable to update user."
end
end
private
def secure_params
params.require(:user).permit(:role, :supervisor_id)
end
end
我遇到的問題是,該腳本沒有成功,但仍然設法更新supervisor_id字段。我嘗試在沒有腳本的情況下測試,看看配對錶條目是否會被創建,但它仍然沒有收到表單中的參數,但是一旦按下按鈕就不會被提交到事務中。不知道該做什麼,真的有什麼幫助表示讚賞!
在瀏覽器中打開控制檯(下開發工具),然後單擊該按鈕來觸發阿賈克斯。瀏覽器控制檯中的錯誤是什麼? – MilesStanfield
控制檯中沒有顯示錯誤,只表示XHR完成加載:PATCH。即使用戶表中的supervisor_id字段得到更新,它看起來像腳本不成功並且執行完畢。這會影響控制器以某種方式創建動作 – Yoklan