Javascript Can';t从表单提交创建新的数据库条目
我有这样的用户和配对模型:Javascript Can';t从表单提交创建新的数据库条目,javascript,ruby-on-rails,ruby,ajax,ruby-on-rails-4,Javascript,Ruby On Rails,Ruby,Ajax,Ruby On Rails 4,我有这样的用户和配对模型: 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
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
我有一个表单视图,其中有一个下拉框列出所有学生,一个列出所有导师。我的目标有两个:
(1) 一旦从框中选择了导师和学生,获取他们的id,并将导师的id关联到学生的“导师id”列。(使用AJAX补丁请求完成)
(2) 用下拉列表中的ID在pairings表中创建一个新行。(通过pairings\u controllers create方法完成)
我的看法是:
<script>
$("#my_special_form").submit(function(){
var url = "/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 %>
$(“#我的特殊表格”)。提交(函数(){
var url=“/users/”+this.elements[“结对[student_id]”值;
$.ajax(url{
发送前:$.rails.CSRFProtection,
数据:{
用户:{
主管id:此.elements[“配对[主管id]”]。值
}
},
键入:“补丁”,
数据类型:“JSON”,
成功:函数(数据、文本状态、jqXHR){
日志('Success',data,textStatus,jqXHR);
},
});
return false;//取消正常表单提交
});
以及控制员:
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
类配对控制器“用户已更新。”
其他的
将\重定向到用户\路径:警报=>“无法更新用户。”
结束
结束
私有的
def安全参数
参数require(:user).permit(:role,:supervisor\u id)
结束
结束
我遇到的问题是,脚本没有成功,但仍然能够更新supervisor\u id字段。我尝试在没有脚本的情况下进行测试,以查看配对表条目是否会被创建,但它仍然没有被创建,因为表单中的参数已被接收,但在按下按钮后未提交到事务。真的不知道该怎么办。谢谢你的帮助 您需要定义控制器响应.json并返回状态代码
respond_to do |format|
if @user.update_attributes(secure_params)
flash[:notice] = 'User was successfully edited.'
format.html { render action: :index }
format.json { render xml: @user, status: 200 }
else
format.html { render action: @user }
format.json { render xml: @user.errors, status: :unprocessable_entity }
end
end
end
或者类似的东西
通过执行以下更改解决
<%= form_for(Pairing.new, url: new_pairing_path, method: :post, html: { id: 'pairing_form' }) do |f| %>
在浏览器中打开控制台(在开发者工具下),然后单击按钮触发Ajax。浏览器控制台中出现了什么错误?控制台中没有显示错误,只显示XHR已完成加载:补丁。即使用户表中的supervisor\u id字段得到了应有的更新,脚本似乎也没有成功并完成执行。这会影响控制器以某种方式创建动作
<%= form_for(Pairing.new, url: new_pairing_path, method: :post, html: { id: 'pairing_form' }) do |f| %>
match '/pairings/new', to: 'pairings#create', via: [:post]
match '/users/update', to: 'users#update', via: [:patch]