0

I'm trying to learn rails 4.1 from rails 4 in action book, I've got to chapter 13, in this chapter defined API for JSON and XML.

It defined a controller like this :

class Api::V1::ProjectsController < Api::V1::BaseController  
  def index  
     respond_with(Project.for(current_user).all)  
  end  
  def create  
    project = Project.new project_params  
    if project.save  
      respond_with(project, :location => api_v1_project_path(project))  
    else  
      respond_with(errors: project.errors.messages)  
    end  
  end  
  private  
  def project_params  
    params.require(:project).permit(:name)  
  end  
end  

And a rspec test like this :

it "unsuccessful JSON" do  
  post "#{url}.json", :token => token, :project => {}  
  expect(last_response.status).to eql(422)  
  errors = {"errors" => { "name" => ["can't be blank"]}}.to_json  
  expect(last_response.body).to eql(errors)  
end  

when I run the test, I get these results :

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

I know, it's because of strong parameters.

m scorpion
  • 21
  • 4

3 Answers3

0

i solved issue like this but any one has a batter idea?

def project_params  
  if params[:project].nil?  
    params[:project] = {:name => ''}  
  end  
  params.require(:project).permit(:name)  
end  
m scorpion
  • 21
  • 4
0

it has resolved this problem in my case

params.require(:project).permit(:name, :description) if params[:project]
Alex Freshmann
  • 481
  • 5
  • 14
0

You can permit nested parameters as explained in strong parameters in github. In your case, this is probably what you want:

params.permit(:project => [:name, :description])
boulder
  • 3,256
  • 15
  • 21