我搜索了高低,发现很多帖子都很相似,但似乎无法调试以下代码。
我有一个项目模型,有很多团队。它有很多用户通过团队。以下是相关模型。
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
我有一个嵌套的表单,应该允许我创建一个新项目和一个团队(只需键入ID),但是我得到一个未允许的参数异常(“未经许可的参数:团队”)
每当我提交表格时。表格代码如下。
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%>
我特别注意允许的参数字段,并认为我把它们弄好了,但是,rails不同意。
任何帮助将不胜感激
答案 0 :(得分:0)
像这样更改此方法并尝试
private
def project_params
params.require(:project)
project_params = params.require(:project).
permit(:name, :code, :description, :externalId,
{:teams_attributes => [:id, :userId, :projectId]})
end
到
private
def project_params
params[:project].permit(:name, :code, :description, :externalId,
{:teams_attributes => [:id, :userId, :projectId]})
end
答案 1 :(得分:0)
您的嵌套表单不适用于has_many
,因为它是单一的。您想要使用f.fields_for :teams do
代替(复数)。请尝试以下更改:
<强> 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%>