Reputation: 634
I have two entities, Project
and Domain
. Project belongs_to Domain
and Domain has_many Projects
. So I populate the form from my project index view
, and if I don't add
accepts_nested_attributes_for :domain
in project.rb
I can see the text-field, if I do add that line, the text-field disappears.
Also I get this in my log files: Unpermitted parameters: domain
.
Project Controller:
def create
@domain = params[:domain][:name]
@domain = Domain.find_or_create_by(name: @domain)
@project = current_user.projects.new(project_params)
@project.domain_id = @domain.id
if @project.save
end
end
def project_params
params.require(:project).permit(:name, :user_id, domain_attributes: [:name])
end
View
<%=simple_form_for @project do |f|%>
<%= f.input :name, label: 'Project name:'%>
<%= f.simple_fields_for :domain do |d|%>
<%= d.input :name, label: 'Domain name:', placeholder: 'domain.co.uk'%>
<%end%>
<%=f.button :submit, class: 'btn btn-success btn-sm'%>
<%end%>
I get this only if I don't add accepts_nested_attributes_for :domain
Parameters: {"utf8"=>"✓", "project"=>{"name"=>"test", "domain"=>{"name"=>"test123.com"}}, "commit"=>"Create Project"}
It is important to have my domain_id
in the projects
. What can I do to fix this.
Upvotes: 1
Views: 265
Reputation: 1349
You need to build a domain so that there is an object to be used in fields_for :domain
in your new action:
def new
@project = ...
@project.build_domain
...
end
The Unpermitted parameters: domain.
message in your log is because in your project_params
method you don't allow a :domain
parameters, but your form is sending one. It should be fine thought, it is just a warning.
Upvotes: 1