简体   繁体   English

Rails 4 has_many通过使用ajax的关联嵌套形式

[英]Rails 4 has_many through association nested form using ajax

I have three models: 我有三种模式:

class Course < ActiveRecord::Base
    validates :title, presence: true

    has_many :enrollments
    has_many :users, through: :enrollments

    accepts_nested_attributes_for :enrollments
end

class Enrollment < ActiveRecord::Base
    belongs_to :user
    belongs_to :course

    enum type: { instructor: 0, student: 1 }
end

class User < ActiveRecord::Base
    has_many :enrollments
    has_many :courses, through: :enrollments
end

Currently when a user creates a course, the association is created as expected (the enrollment object is created as well). 当前,当用户创建课程时,将按预期方式创建关联(也将创建注册对象)。 However, I'm trying to figure out the most conventional way to immediately assign the enrollment object's type to be 0. 但是,我试图找出最常规的方法立即将注册对象的类型分配为0。

Because I'm using React as my front-end framework, I attempt this through an ajax request. 因为我将React用作前端框架,所以我尝试通过ajax请求进行此操作。

var NewCourseForm = React.createClass({

  submit: function() {
    var params = {
      course: {
        title: this.refs.title.getDOMNode().value,
        enrollments_attributes: {
          type: 0
        }
      }
    };

    $.ajax({
      type: 'POST',
      url: '/courses',
      data: params,
      success: this.handleData
    });
  },

  handleData: function(response) {
    console.log(response);
  },

  render: function() {
    return (
      <div>
        <h1>New Course</h1>

        <input type='text' placeholder='Title' ref='title' />

        <br />

        <button onClick={this.submit}>Submit</button>
      </div>
    );
  }

});

This is my courses_controller.rb 这是我的courses_controller.rb

class CoursesController < ApplicationController
  def index
    @courses = Course.all
  end

  def show
    @course = Course.find(params[:id])
  end

  def new
  end

  def create
    @course = current_user.courses.create(course_params)

    respond_to do |format|
      format.html { redirect_to action: :index }
      format.json { render json: @course }
    end
  end

  private

  def course_params
    params.require(:course).permit(:title, enrollments_attributes: [:type])
  end
end

Right now I get an error that says: 现在,我收到一条错误消息:

Completed 500 Internal Server Error in 23ms (ActiveRecord: 2.8ms)

TypeError (no implicit conversion of Symbol into Integer):

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks! 谢谢!

This may be a bit late. 这可能有点晚了。 But why don't you set it in your controller create action after it is passed through via ajax? 但是,为什么不通过ajax将其设置在控制器的create操作中呢? Something like 就像是

def create
    @course = current_user.courses.create(course_params)
    @course.type = 0

    respond_to do |format|
      format.html { redirect_to action: :index }
      format.json { render json: @course }
end

You can also change your enrollment class with enums to be like this: 您也可以使用枚举更改注册类别,如下所示:

Class Enrollment
    enum type: [:teacher, :student]

The code above automatically assigns 0 and 1 to each role. 上面的代码自动为每个角色分配0和1。 Subsequently you can change your schema so it auto assigns a student role upon enrollment. 随后,您可以更改架构,以便在注册时自动分配学生角色。 That way the only thing that is assigned is the teacher role upon hitting the create action in your enrollments controller. 这样,分配的唯一内容就是在您的注册控制器中单击“创建”操作时的老师角色。

t.integer  "type",                default: 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails 4通过关联和数据库嵌套表格has_many - Rails 4 Nested form has_many through association and database Rails form_for has_many通过使用AJAX的关联不起作用 - Rails form_for has_many through association using AJAX not working has_many多对多:通过关联嵌套形式 - many-to-many with has_many :through association nested form Rails has_many:通过嵌套表单 - Rails has_many :through nested form Rails 3 - has_many通过嵌套表单 - Rails 3 - has_many through with nested form 如何通过使用simple_form和嵌套表单的关联为has_many创建嵌套表单? - How to create a nested form for a has_many through association using simple_form and nested form? 通过关联未创建具有has_many的Rails嵌套属性 - Rails Nested Attributes with has_many through association not creating 绝对坚持尝试使用has_many来创建rails形式的嵌套关联 - Absolutely stuck trying to create nested association in rails form with has_many through 如何使用has_many为模型制作嵌套表单:通过Rails 5中的关联 - How to make nested form for model with has_many :through association in Rails 5 “无效的关联”:nested_form_for与has_many - “Invalid association”: nested_form_for with has_many through
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM