Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/468.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 按钮从视图调用控制器操作以更新数据库列_Javascript_Ruby On Rails_Ajax_Postgresql_Ruby On Rails 4 - Fatal编程技术网

Javascript 按钮从视图调用控制器操作以更新数据库列

Javascript 按钮从视图调用控制器操作以更新数据库列,javascript,ruby-on-rails,ajax,postgresql,ruby-on-rails-4,Javascript,Ruby On Rails,Ajax,Postgresql,Ruby On Rails 4,我试图在单击按钮时将用户添加到表列(PostgreSQL)中的数组中。按钮点击注册,但没有调用控制器方法,也没有给出错误 我有下面这个按钮的视图 views/users/index.html.erb <%= form_for(Pairing.new, url: users_path, method: :patch, html: { id: 'my_special_form' }) do |f| %> <div class="field"> <%= f.l

我试图在单击按钮时将用户添加到表列(PostgreSQL)中的数组中。按钮点击注册,但没有调用控制器方法,也没有给出错误

我有下面这个按钮的视图

views/users/index.html.erb

<%= form_for(Pairing.new, url: users_path, method: :patch, 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>
  <%= f.submit %>
<% 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

  def destroy
    user = User.find(params[:id])
    authorize user
    user.destroy
    redirect_to users_path, :notice => "User deleted."
  end

  def pair
    supervisor = User.find(params[:id1])
    student = User.find(params[:id2])

    studentlist = supervisor.select(:supervised_students)
    studentlist << student.
    supervisor.update_attribute(:supervised_students => studentlist)
    @supervisor.save
  end

  private

  def secure_params
    params.require(:user).permit(:role)
  end

end
控制器/用户\u控制器

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

  def destroy
    user = User.find(params[:id])
    authorize user
    user.destroy
    redirect_to users_path, :notice => "User deleted."
  end

  def pair
    supervisor = User.find(params[:id1])
    student = User.find(params[:id2])

    studentlist = supervisor.select(:supervised_students)
    studentlist << student.
    supervisor.update_attribute(:supervised_students => studentlist)
    @supervisor.save
  end

  private

  def secure_params
    params.require(:user).permit(:role)
  end

end
Routes.rb

Rails.application.routes.draw do

  devise_for :users

  resources  :users

  resources  :pairings

  resources  :conversations, only: [:index, :show, :destroy] do
    member do
      post :reply
    end
  end

  resources :conversations, only: [:index, :show, :destroy] do
    member do
      post :restore
    end
  end

  resources :conversations, only: [:index, :show, :destroy] do
    collection do
      delete :empty_trash
    end
  end

  resources :conversations, only: [:index, :show, :destroy] do
    member do
      post :mark_as_read
    end
  end

  resources  :messages, only: [:new, :create]

  devise_scope :user do
    authenticated :user do
      root 'pages#index', as: :authenticated_root
    end

    unauthenticated do
      root 'pages#welcome', as: :unauthenticated_root
    end
  end



  root to: 'pages#index'

end
URI中的
#
用于片段标识符-根据规范,它对服务器是透明的

因此,当您发送ajax请求时,它实际上被解释为
POST/users

root
用于设置
/
路径的处理程序。如果您想添加
POST users/pair
,您可以这样做

resources :users do
   collection do
     post 'pair', to: "users#pair"
   end
end  
请注意,routes中的
#
表示
控制器#操作
,与结果无关

然而,这是我处理问题的方式

建立关系 确保在
用户
上有一个
主管id

class User < ActiveRecord::Base
  has_many :students, class_name: "Users",
                      foreign_key: "supervisor_id"

  belongs_to :supervisor, class_name: "User"
end
形式 如果你只是简单地列出学生,你可以使用这样一个简单的表格,你会很乐意去:

<%= form_for(user) do |f| %>
  <%= f.collection_select :supervisor, User.where(role: 1) %> 
  <%= f.submit %>
<% end %>
这是一个不受数据库表支持的模型。我们可以简单地使用它来绑定表单 投入:


我想我已经设法将数据从视图发送到javascript。我认为我的问题是将数据从javascript发送到我的控制器Action。我目前正在按照您解释的方式进行尝试,但是在您使用的用户模型中有很多关系。我的学生角色用户只能有一个与他们相关的主管,因此我是否应该将相关的主管改为有一个?谢谢你的详细回答顺便说一句!啊,从你的表现来看,我猜这是多对多。将编辑答案。希望能够将一名导师与许多学生配对,但一名学生只能有一名导师。我已将您提供的表单放入我的users/index.html.erb文件中,但它给出了一个“未定义的方法`local_assignments'”错误。是否必须将其放置在pairings/_form.html.erb目录中才能正常工作?再次感谢:)由于它是围绕多对多关系建立的,所以它仍然不会起作用。我正在进行编辑。单击“配对”按钮后,它会将我链接到我的用户配置文件页面,并说1个错误禁止保存此用户(设计用户视图)。在chrome控制台上,它显示错误“uncaughtSyntaxError:意外标识符”,指向javascript文件中的“type:'PATCH',”行。用当前代码编辑我的原始问题。
class User < ActiveRecord::Base
  has_many :students, class_name: "Users",
                      foreign_key: "supervisor_id"

  belongs_to :supervisor, class_name: "User"
end
PATCH /users/5 { supervisor_id: 6 }
<%= form_for(user) do |f| %>
  <%= f.collection_select :supervisor, User.where(role: 1) %> 
  <%= f.submit %>
<% end %>
# app/models/pairing
class Pairing
  include ActiveModel::Model
  attr_accessor :supervisor_id, :student_id
end
<%= form_for(Pairing.new, url: users_path, method: :patch, 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 :supervisor_id %>
    <%= f.collection_select(:student_id, User.where(role: 0), :id, :name) %>
  </div>
  <%= f.submit %>
<% end %>
$("#my_special_form").submit(function(){

  var url = this.action + "/" + 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
});