Reputation: 51
I have two models, ProjectCategory
and ProjectQuestion
. A ProjectCategory
can have many questions related to it, and a ProjectQuestion
can only have one category related to it.
I have a form letting the user edit a question, including what category (if any) it's related to. When I submit the form however, I get the following error:
Unpermitted parameter: project_category_id
However in the ProjectQuestion
controller I have the permit method configured like so:
params.require(:project_question).permit(:title, :project_category_id)
I can't figure out how to let me set the foreign key. All the SO answers I've found so far only show the situation where you're creating the relationship on the parent side which isn't what I want.
Started PATCH "/admin/questions/2" for ::1 at 2017-04-21 16:45:19 +1000
Processing by AdminController#update_project_question as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"9Abl7Rruj/5LD4/rI/WKlQ8MQ8eY/TMVFUaw53kfFw199xFekfzgyiWZytzsJ5qHU+edNpoqhXG5xWvuzHFP7g==", "project_question"=>{"title"=>"What are the images not telling us testing", "project_category_id"=>"9"}, "commit"=>"Update Project question", "id"=>"2"}
<%= f.label 'Category' %>
<%= f.select :project_category_id, options_for_select(ProjectCategory.all.map{|s|[s.title, s.id]}) %>
ProjectCategory
model:class ProjectCategory < ActiveRecord::Base
belongs_to :entry
has_many :project_questions
validates_presence_of :title
end
ProjectQuestion
model:class ProjectQuestion < ActiveRecord::Base
has_many :entry_project_questions
validates_presence_of :title, :project_category_id
belongs_to :project_category
end
This is my first Ruby project so I'm still learning, but I've been unable to figure out what I'm missing to get this to work properly.
Upvotes: 0
Views: 605
Reputation: 116
Hello @Christopher Muller,
You are processing the request through AdminController, whereas you have put permit in ProjectQuestion. Please add the code to AdminController where the processing is happening.
Thanks.
Upvotes: 1