1

我正在尝试使用用户模型和行业模型创建一个 rails 4 应用程序。它们之间有一个 has_and_belongs_to_many 关联。我根据指南http://guides.rubyonrails.org/association_basics.html创建了连接表, 我得到了:industry_ids. 所以我遵循了关于强参数的设计部分

class ApplicationController < ActionController::Base
  before_filter :configure_permitted_parameters, if: :devise_controller?

  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) << :about
    devise_parameter_sanitizer.for(:sign_up) << :industry_ids
  end
end

但是当我在这里阅读http://blog.sensible.io/2013/08/17/strong-parameters-by-example.html时,对于这样的关联,我需要告诉 rails 这是一个数组。

如何修复创建具有:industries关联的用户?

4

1 回答 1

4

我最终使用了一个块。

class ApplicationController < ActionController::Base
  before_filter :configure_permitted_parameters, if: :devise_controller?

  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) { |u|
      u.permit(:email, :password, :password_confirmation,
               :about, industry_ids: []) }
  end
end
于 2013-09-24T03:10:51.627 回答