last day (15 days later) » 

8:31 PM
0
A: Unpermitted parameter in Rails 5

Arup RakshitReplace project_criteria: [.. with project_criteria_attributes: [... When you will be using accepts_nested_attributes_for, that is the key name Rails expects you to be defined both from frontend and whitelisting code.

 
Thanks for you response.. do you mean I should replace the code in controller to: params.require(:project).permit(:name, project_criteria_attributes: [] ) ?
 
Yes please try that and let me know. Your JSON params also has to be the same key name.
 
Unfortunately I got the same error. Parameters: {"project"=>{"name"=>"qweqwe", "project_criteria_attributes"=>{"0"=>{"benefit"=>"1", "name"=>"qweqwe", "type"=>"Benefício"}}}} Unpermitted parameter: project_criteria_attributes.
 
@developer033 I didn't see 1 thing. project_criteria has to be in plural format in the model.
@developer033 call in console "project_criteria".pluralize and see what name comes and use it if you are not sure what the plural name would be. And then that pluralize name and _attributes will do the trick.
 
Yes, it's in the plural form. My model is ProjectCriterium (singular) andcriteria is the plural form.
 
8:31 PM
Did you try the method I said? ProjectCriterium.to_s.underscore.pluralize
 
Yes, ProjectCriterium.to_s.underscore.pluralize === project_criteria..
 
hello
 
hm looking at what is missing
 
Do you think the way that I'm getting the data is wrong? => Parameters: {"project"=>{"name"=>"qweqwe", "project_criteria_attributes"=>{"0"=>{"benefit"=>"1", "name"=>"qweqwe", "type"=>"Benefício"}}}} Unpermitted parameter: project_criteria_attributes.
I was expecting an array.. by the way
 
8:33 PM
your current syntax like key/value also must work
show your controller code here once more time
mainly the strong param method
 
This way I get the unpermitted message:

def project_params
params.require(:project).permit(:name, project_criteria_attributes: [] )
end

This way I got ActiveRecord::DangerousAttributeError (attribute is defined by Active Record. Check to make sure that you don't have an attribute or method with the same name.):

def project_params
params.require(:project).permit(:name, project_criteria_attributes: [:name, :type, :benefit] )
end
 
ok
show the create action of the controller
 
the create action is the standard.. I just generate it now

def create
@project = Project.new(project_params)

if @project.save
render json: @project, status: :created, location: @project
else
render json: @project.errors, status: :unprocessable_entity
end
end
First of all I want to permit that parameter
then I'll decide what's the best way to do what I want
 
i know
show the full log now
from request to response
 
Started POST "/projects" for 127.0.0.1 at 2016-08-19 17:39:34 -0300
Processing by ProjectsController#create as HTML
Parameters: {"project"=>{"name"=>"qwe", "project_criteria_attributes"=>{"0"=>{"benefit"=>"1", "name"=>"aaaaaa", "type"=>"type1"}}}}
Unpermitted parameter: project_criteria_attributes
Are the classes correct?

class ProjectCriterium < ApplicationRecord
belongs_to :project
end

class Project < ApplicationRecord
has_many :project_criteria
accepts_nested_attributes_for :project_criteria
end
 
8:43 PM
Why your request is being submitted a HTML, shouldn't be a JSON request. Can you check?
 
I noticed that, but do you think is it a problem? Is it responsability of back-end or front-end?
 
yeah
can you submit a JSON reqest
is it an API?
 
yes, it's
I'm using Angular in front-end
 
ok fix what I said
 
Well, I really don't know why rails is processing it as HTML
return $http({
method: 'POST',
url: 'http://localhost:3000/projects',
data: {
'project': project
}
});
by default POST in Angular is sent using JSON
 
8:50 PM
funny thing is why Rails not telling any view not found error, I am surprised. :)
for testing
can you send the reqqyest from Postman as JSON and check what is going on
this is to test the thing
 
okay
I tested it
and it works
as post
I passed it:

{"project":
{
"name": "qwe",
"project_criteria_attributes":{
"0":{ "benefit":"1", "name":"aaaaaa", "type":"type1" }
}
}
}
it returned me
{
"id": 57,
"name": "qwe",
"created_at": "2016-08-19T21:00:18.000Z",
"updated_at": "2016-08-19T21:00:18.000Z"
}
and in terminal:
Started POST "/projects" for 127.0.0.1 at 2016-08-19 18:00:18 -0300
Processing by ProjectsController#create as */*
Parameters: {"project"=>{"name"=>"qwe", "project_criteria_attributes"=>{"0"=>{"benefit"=>"1", "name"=>"aaaaaa", "type"=>"type1"}}}}
Unpermitted parameter: project_criteria_attributes
 
u mean record is saving but still error?
 
no
I mean I still can't access project_criteria_attributes
even with postman
 
9:41 PM
ok
 
10:39 PM
are you there?
 

last day (15 days later) »