Ember CLI 0.2.5 + Rails 4.2 using ember-cli-rails gem, active_model_serializer (0.9.3)
I am currently trying to pass user into createRecord and it won't save or bind the user_id when passing to rails. Parameters come back as:
{"course"=>{"subject"=>"CSE", "number"=>110, "credit_hours"=>3, "grading_scale"=>"Regular", "user_id"=>nil}}
Ember Controller (Finding the user returns correct value)
import Ember from 'ember';
export default Ember.Controller.extend({
scales: ['Regular', 'Plus/Minus', 'Plus', 'Minus'],
weights: [],
actions: {
createCourse: function() {
var self = this;
var course = this.get('model');
this.store.find('user', this.get('session.content.secure.id')).then(function(user) {
course.set('user', user);
})
course.save().then(function(course){
$.growl.notice({title: 'Course', message: 'Sucessfully created course.'})
})
}
}
});
Rails Controller
class CoursesController < ApplicationController
def index
@courses = User.find(params[:user_id]).courses
render json: @courses
end
def show
@course = Course.find(params[:id])
render json: @course
end
def update
@course = Course.find(params[:id])
if @course.update_attributes(params[:course])
render json: @course
else
render json: @course.errors, status: 500
end
end
def create
@course = Course.new(course_params)
if @course.save
render json: @course, status: :created
else
render json: @course.errors, status: 500
end
end
private
def course_params
params.require(:course).permit(:subject, :number, :credit_hours, :grading_scale, :user_id)
end
end
Serializer (I had a stack level too deep error here so i used user_id which i have seen done before, I even downgraded to 0.8.3 to fix that error and still no luck)
class CourseSerializer < ActiveModel::Serializer
embed :ids, include: false
attributes :id, :subject, :number, :credit_hours, :grading_scale, :user_id
#has_one :user
has_many :weights
has_many :grades
end
Route (Pretty straight forward here)
import Ember from 'ember';
import AuthenticatedRouteMixin from 'simple-auth/mixins/authenticated-route-mixin';
export default Ember.Route.extend(AuthenticatedRouteMixin, {
model: function() {
var store = this.store;
return store.createRecord('course');
},
setupController: function(controller, model) {
controller.set('model', model);
},
actions: {
willTransition: function() {
var course = this.controller.get('model');
course.rollback();
}
}
});
Snippet of Course Model
export default DS.Model.extend({
user: DS.belongsTo('user'),
weights: DS.hasMany('weight',{ async: true }),
grades: DS.hasMany('grade',{ async: true }),
subject: DS.attr('string'),
number: DS.attr('number'),
creditHours: DS.attr('number'),
gradingScale: DS.attr('string')
Snippet of User Model
export default DS.Model.extend({
courses: DS.hasMany('course'),
grades: DS.hasMany('grade'),
email: DS.attr('string'),
name: DS.attr('string'),
gradePoints: DS.attr('number'),
gradeUnits: DS.attr('number'),
password: DS.attr('string'),
password_confirmation: DS.attr('string')
Let me know if i need to supply anything else. This has been frustrating me for awhile.