gpt4 book ai didi

javascript - Ember ownsTo 关系在保存时传递为 nil 到服务器

转载 作者:行者123 更新时间:2023-12-02 16:05:40 26 4
gpt4 key购买 nike

Ember CLI 0.2.5 + Rails 4.2 使用 ember-cli-rails gem、active_model_serializer (0.9.3)

我目前正在尝试将用户传递到createRecord,并且在传递到rails时它不会保存或绑定(bind)user_id。返回的参数为:

{"course"=>{"subject"=>"CSE", "number"=>110, "credit_hours"=>3, "grading_scale"=>"Regular", "user_id"=>nil}}

Ember Controller (查找用户返回正确值)

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

序列化器(我这里有一个堆栈级别太深的错误,所以我使用了我之前见过的 user_id,我什至降级到 0.8.3 来修复该错误,但仍然没有运气)

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

路线(这里非常简单)

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();
}
}
});

类(class)模型片段

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')

用户模型片段

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')

如果我需要提供其他东西,请告诉我。这让我沮丧了一段时间。

最佳答案

createCourse方法中course.save()user promise 得到解决之前运行,这就是为什么你看到user_id: nil。这会起作用:

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);
return course.save();
}).then(function(course) {
$.growl.notice({title: 'Course', message: 'Sucessfully created course.'});
});
}

关于javascript - Ember ownsTo 关系在保存时传递为 nil 到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30785474/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com