Ember Data无法通过createRecord正确地将params传递给Rails

时间:2017-12-16 22:59:29

标签: ruby-on-rails ember.js

我尝试设置Ember Data调用来创建一个注册对象,该对象只包含一封电子邮件,仅此而已。我的Ember Data设置有点时髦,因为params似乎已经通过但是Rails无法接收它们。不确定是什么错误。

Ember前端:

  var signup = this.get('store').createRecord('signup', {
    email: this.get('emailAddress')
  });
  console.log(this.get('emailAddress')); --> prints fine
  console.log(signup); --> data is empty, somewhat alarming/confusing
  console.log(signup.get('email')); --> however, this also prints fine
  signup.save();

请求有效负载:

{"data":{"attributes":{"email":"gg@gg.com"},"type":"signups"}}

Rails后端:

  def create
    @signup = Signup.new({ email: params[:email] })
    p params
    # prints <ActionController::Parameters {"controller"=>"signups", "action"=>"create"} permitted: false>

    p @signup --> prints Signup object with nil for email
    @signup.save!
    render json: @signup
  end

响应有效负载:

{"data":{"id":"3","type":"signup","attributes":{"email":null}}}

1 个答案:

答案 0 :(得分:1)

我需要将此行添加到config/initializers/mime_types.rb

Mime::Type.register "application/vnd.api+json", :json

因为我使用JSON API规范从Ember传递数据所以我需要让Rails正确处理它。

查看更多:https://github.com/rails/rails/issues/28564