我正在尝试使用用户模型和行业模型创建rails 4应用程序。它们之间有一个has_and_belongs_to_many关联。我根据指南http://guides.rubyonrails.org/association_basics.html创建了联接表
我正在为:industry_ids
获取未经许可的参数。所以我按照strong parameters
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
关联来修复用户的创建?
答案 0 :(得分:3)
我最终使用了一个块。
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