У меня есть три модели:Rails 4 has_many через объединение вложенной формы с помощью Ajax
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
В настоящее время, когда пользователь создает курс, объединение создается, как и ожидалось (объект регистрации создается, а). Тем не менее, я пытаюсь выяснить наиболее обычный способ немедленного присвоения типа объекта регистрации 0. 0.
Поскольку я использую 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>
);
}
});
Это мой 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
Сейчас я получаю сообщение об ошибке, которая говорит:
Completed 500 Internal Server Error in 23ms (ActiveRecord: 2.8ms)
TypeError (no implicit conversion of Symbol into Integer):
Любая помощь будет оценена. Благодаря!