1

我想创建一个用户模型和行业模型的轨道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,用于关联这样我要告诉轨,这是一个数组。

如何解决使用:industries关联创建用户的问题?

回答

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