Strong params require works, but permit doesn't

1.7k views Asked by At

Here's the relevant Rails code:

def update
  project_id = update_params[:project]
end

def update_params
  params.require(:project).permit(:technology, :team_member)
end

And here's my PATCH request (Angular + CoffeeScript):

Restangular.all("projects/#{$scope.projectID}")
  .patch(team_member_delete: team_member.id, project: $scope.projectID)

Here's what the server sees:

Parameters: {"technology_delete"=>7, "project"=>"64", "id"=>"64"}

And here's the full error from the server log:

Started PATCH "/projects/64" for ::1 at 2015-06-12 17:23:40 -0400
Processing by ProjectsController#update as HTML
Parameters: {"technology_delete"=>7, "project"=>"64", "id"=>"64"}
Completed 500 Internal Server Error in 1ms (ActiveRecord: 0.0ms)

NoMethodError (undefined method `permit' for "64":String):
  app/controllers/projects_controller.rb:73:in `update_params'
  app/controllers/projects_controller.rb:29:in `update'

The part that says, "undefined method `permit' for "64":String" makes me think it's interpreting 64 as a key, rather than a value.

Here are some things I've tried:

  • Formatting patch request parameters as JSON (no change)
  • Accessing params["project"] instead of params[:project] (no change)
  • Sending 64 (an int) rather than "64" (a string) (undefined method `permit' for 64:Fixnum)

I'm in rails 4.2.1, so strong_params should be pre-installed without a gem. This question looked promising, at first, but the server log shows that the params are a hash, not a string. Rails 4 Controller Test "Undefined Method Permit"

2

There are 2 answers

0
Mark Swardstrom On BEST ANSWER

Looks like you have two problems

First - This is odd

def update
  project_id = update_params[:project]
end

Usually, it looks like

def update
  project = Project.find(id)
  project.update_attributes(project_params)
  ...
end

private

def project_params
  params.require(:project).permit(:technology, :team_member)
end

And second - your form isn't submitting the project params in the right format - looks like the entire project is missing. As @joseph said, they should be in a hash. Add the form view code if you need some help.

0
Joseph Freivald On

Try

params.require(:project).permit(:technology, :project, :team_member)

...

Actually, it looks like you need to have the parameters passed like this:

"project"=>{"technology_delete"=>7, "project"=>"64", "id"=>"64"}

first