has_many的未允许参数:通过Rails 5中的嵌套表单

时间:2017-02-24 10:08:30

标签: ruby-on-rails ruby ruby-on-rails-5

您好我正在尝试通过我的Rails 5应用程序中的嵌套表单实现has_many:来创建一个分配给它的用户的项目。但是,当我尝试创建项目时,我面临以下验证错误:

- Project users user company must exist
- Project users user email can't be blank
- Project users user email is invalid
- Project users user password can't be blank
- Project users user firstname can't be blank
- Project users user lastname can't be blank

这些是我为用户模型所做的验证,但我不明白为什么当我使用嵌套用户创建项目时它们会出现。此外,控制台指示:fullname为未允许的参数,如下所示。

Parameters: {"utf8"=>"✓", "authenticity_token"=>"3RtrzKKVrSOiGLYnHwubF3GaRAbcQE/61doRM5clT8GpFYeJoEVnU2lEmQsNwVO8qecdqig8xjwbTqqaqL1gYQ==", "project"=>{"name"=>"dsada", "description"=>"dadasd", "project_users_attributes"=>{"0"=>{"user_attributes"=>{"fullname"=>["", "5", "4", "3"]}}},"choice"=>"1", "button"=>"", "company_id"=>"1"}
User Load (0.9ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
Company Load (0.3ms)  SELECT  "companies".* FROM "companies" WHERE "companies"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
Unpermitted parameter: fullname

有没有人可以告诉我为什么我面临这些验证错误以及为什么全名被认为是不允许的,尽管我将它们包含在项目强大的参数中?我的代码如下所示。谢谢!

#Model - project.rb
class Project < ApplicationRecord
  has_many :project_users, inverse_of: :project
  has_many :users, through: :project_users
  accepts_nested_attributes_for :project_users
end

#Model - project_user.rb
class ProjectUser < ApplicationRecord
  belongs_to :project, inverse_of: :project_users
  belongs_to :user, inverse_of: :project_users
  accepts_nested_attributes_for :user
end

#Model - user.rb
class User < ApplicationRecord
  has_many :project_users, inverse_of: :user
  has_many :projects, through: :project_users
end

#Controller - projects_controller.rb
class ProjectsController < ApplicationController
  def new
    @company = Company.find(params[:company_id])
    @project = @company.projects.build
    @project.project_users.build.build_user
  end

  def create
    @company = Company.find(params[:company_id])
    @project = @company.projects.build(proj_params)
    if @project.valid?
      @project.save
      render 'show'
    else
      render 'new'
    end
  end

  private
    def proj_params
      params.require(:project).permit(:name, :description, project_users_attributes: [:id, user_attributes:[:fullname]])
    end
  end
end

#Views - new.html.erb

<%= form_for [@company, @project] do |f| %>
  <%= f.label :title %>
  <%= f.text_field :name %>
  <%= f.label :description %>
  <%= f.text_area :description %>
  <%= f.fields_for :project_users do |project_user_form| %>
    <%= project_user_form.fields_for :user do |user| %>
      <%= user.label :team_members %>
      <%= user.select :fullname, company.users.collect { |p| [ p.fullname, p.id ] }, {:prompt => 'Select Team Members'}, { :multiple => true} %>
    <% end %>
  <% end %>
  <%= f.button :submit => "" %>
<% end %>

1 个答案:

答案 0 :(得分:-1)

这就是为什么你的强参数效果不好的原因。 您在proj_params中说:fullname是一个字段。但它不仅仅是一个领域 - 它是一系列价值观。因此,如果您将其更改为:fullname => [],则允许参数。

但是......这没有任何意义。 您的代码正在尝试创建新用户不分配。这就是您获得验证错误的原因。防止这种情况的最简单方法是传递用户的ID,这将尝试更新/构建关联,而不是创建。 然后,你最好通过用户&#39;视图中的ID。尝试传递user_ids而不是全名,不要忘记强参数中的user_ids => []