0

I'm trying to build a Rails-based API to be consumed by an ember app. I'm working on project creation, and I'm running into some problems with strong parameters:

class ProjectsController < ApplicationController
  def create
    respond_with current_user.projects.create project_params
  end

private
  def project_params
    params.require(:project).permit(:name)
  end
end

So here I've got a controller that should accept a parameter has with a project object and the other details embedded in the project object.

This falls apart when I submit an empty form, which submits as {"project" => {}}., which throws

ActionController::ParameterMissing Exception: param is missing or the value is empty

I believe requiring the top level parameter is considered idiomatic, but it seems that the way ember removed empty parameter from the payload will cause problems in this case. How should this be handled?

DVG
  • 17,392
  • 7
  • 61
  • 88

2 Answers2

0

The easiest way to use Ember with Rails is to use Ember Data's ActiveModelAdapter/ActiveModelSerializer in your Ember code, and active_model_serializers in your Rails code.

There are some notes at the top of the Ember links describing what settings are expected from a_m_s.

Sam Selikoff
  • 12,366
  • 13
  • 58
  • 104
  • I am doing all of those things. – DVG Dec 07 '14 at 00:56
  • Is there any param that's nonempty? – Sam Selikoff Dec 07 '14 at 03:41
  • The issue ended up being that I hadn't defined a null property in the controller for each param, it appears if you don't do that, they're undefined and don't get sent unless you fill something in on the form side – DVG Dec 07 '14 at 13:14
0

The problem here was I had the ember controller set up like this:

New = Ember.Controller.extend
  actions:
     createProject: ->
       @store.createRecord 'project',
         name: @get('name')

This appeared to be fine, however when name wasn't filled out in the form template, it would be undefined and wouldn't be sent. I was able to fix it by just adding:

New = Ember.Controller.extend
  name: null
  # rest the same
DVG
  • 17,392
  • 7
  • 61
  • 88