【问题标题】:ember rails inserting data with empty fieldsember rails 插入带有空字段的数据
【发布时间】:2013-10-11 11:04:54
【问题描述】:

这是我在模板中的表单

<form>
  {{view Ember.TextArea valueBinding="comments" placeholder="Please type your comment here"}}
  <div class="form-footer">
   <button type="submit" class="btn pull-right btn-primary" tabindex="100" {{action saveIndianize}}>Save</button> 
  </div>
</form>

这是我的 js 模型

App.Comment = DS.Model.extend({
    post_id: DS.attr('number'),
    user_id: DS.attr('number'),
    comments: DS.attr('string'),
    created_at: DS.attr('date'),
    job: DS.belongsTo('App.Post',{embedded:true}),
});

这是我的序列化器

attributes :id,:post_id,:user_id,:comments,:created_at

这是我的 Rails 控制器

@comment = Comment.new(user_id: params[:user_id],post_id: params[:post_id],comments: params[:comments])

当我提交表单时抛出错误

Uncaught Error: assertion failed: Your server returned a hash with the key comments but you have no mapping for it

它使用 id(主键)、created_at 和 updated_at 插入数据库。但我看不到user_id, post_id 和comments。

我该如何解决。

【问题讨论】:

  • ember.js 和 ember-data 的版本是多少?

标签: ember.js ember-data ember-rails


【解决方案1】:

我怀疑你有两个问题。

首先,我认为user_id、post_id 和comments 可能由于批量分配保护而未设置。如果您使用的是 Rails 4,则需要查看 Strong Parameters,如果您使用的是 Rails 3,则需要调查 attr_accessible。

错误与您返回的 JSON 格式有关。您需要确保 JSON 返回主键 comment(单数),而不是 comments(复数)。

{ "comment" : {...} }

不是:

{ "comments" : {...} }

【讨论】:

    【解决方案2】:

    我的控制器中的更改已解决

    @comment = Comment.new(user_id: params[:comments][:user_id],post_id: params[:comments][:post_id],comments: params[:comments][:comments])

    【讨论】:

      猜你喜欢
      • 2015-03-27
      • 2014-08-29
      • 2013-04-19
      • 1970-01-01
      • 2012-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多