Question

I've searched high and low, and found many posts similiar, but can't seem to debug the following code.

I have a project model, which has many many teams. It has many users through teams. Here are the relevant models.

project.rb

 class Project < ActiveRecord::Base
   has_many :teams
   has_many :users, :through =>  :teams

   accepts_nested_attributes_for :teams
 end

team.rb

class Team < ActiveRecord::Base
  belongs_to :project
  belongs_to :user
end

user.rb

 class User < ActiveRecord::Base
   has_many :teams
   has_many :projects, :through => :teams
 end

projects_controller.rb

class ProjectsController < ApplicationController
  def index
    @projects = Project.all
  end

  def new
    @project = Project.new
  end

  def create
    @project = Project.create(project_params)
    redirect_to projects_url
  end

  def show
    @project = Project.find(params[:id])
  end

  def edit
    @project = Project.find(params[:id])
  end

  def update
    @project = Project.find(params[:id])
    @project.update(type_params)
    redirect_to project_url
  end

  def destroy
    @project = Project.find(params[:id])
    @project.destroy
    redirect_to projects_url
  end

end

private

def project_params
  params.require(:project)
  project_params = params.require(:project).
  permit(:name, :code, :description, :externalId,
       {:teams_attributes => [:id, :userId, :projectId]})
end

I have a nested form, that should allow me to create a new project and one team (just typing in the ID), however I get an unpermitted parameters exception ("Unpermitted parameters: team")

Whenever I submit the form. Form code is below.

new.html.erb

<h1>Create New Project</h1>

<%=form_for(@project) do |f| %>

<p>
  <%=f.label "Name"%>
  <%=f.text_field :name%> <br>

  <%=f.label "Code"%>
  <%=f.text_field :code%> <br>

  <%=f.label "External ID"%>
  <%=f.text_field :externalId%> <br>

  <%=f.label "Description"%>
  <%=f.text_field :description%> <br>
</p>

<ul>
  <%= f.fields_for :team do |tf| %>
      <li>
        <%= tf.label 'User Id' %>
        <%= tf.text_field :userId %>
        <%= tf.label 'Project Id' %>
        <%= tf.text_field :projectId %>
      </li>
</ul>
  <%end%>
 <%=f.submit%>

 <%end%>

I've paid special attention to the permitted parameters fields, and think I got them right, but, rails disagrees.

Any help would be appreciated

Was it helpful?

Solution

Your nested form doesn't work with has_many because it is singluar. You want to use f.fields_for :teams do instead (plural). Please try the following changes:

project_controller.rb

def new
  @project = Project.new
  @project.teams.build
end

[...]
private
  [...]
  # Never trust parameters from the scary internet, only allow the white list through.
  def project_params
    params.require(:project).permit(:name, :code, :externalId, :description, teams_attributes: [ :user_id, :project_id ])
  end

new.html.erb

<h1>Create New Project</h1>

<%=form_for(@project) do |f| %>
  <p>
    <%=f.label "Name"%>
    <%=f.text_field :name%> <br>

    <%=f.label "Code"%>
    <%=f.text_field :code%> <br>

    <%=f.label "External ID"%>
    <%=f.text_field :externalId%> <br>

    <%=f.label "Description"%>
    <%=f.text_field :description%> <br>
  </p>

  <ul>
    <%= f.fields_for :teams do |tf| %>
      <li>
        <%= tf.label 'User Id' %>
        <%= tf.text_field :user_id %>
        <%= tf.label 'Project Id' %>
        <%= tf.text_field :project_id %>
      </li>
    <%end%>
  </ul>
  <%=f.submit%>
<%end%>

OTHER TIPS

Change this method like this and try

private

def project_params
  params.require(:project)
  project_params = params.require(:project).
  permit(:name, :code, :description, :externalId,
       {:teams_attributes => [:id, :userId, :projectId]})
end

to

private

def project_params
  params[:project].permit(:name, :code, :description, :externalId,
       {:teams_attributes => [:id, :userId, :projectId]})
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top