【问题标题】:unpermitted parameter in has and belongs to many association有并且属于许多关联中的未经允许的参数
【发布时间】:2013-09-24 02:40:52
【问题描述】:

我正在尝试使用用户模型和行业模型创建一个 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 关联的用户?

【问题讨论】:

    标签: devise ruby-on-rails-4 has-and-belongs-to-many strong-parameters


    【解决方案1】:

    我最终使用了一个块。

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-20
      • 1970-01-01
      • 1970-01-01
      • 2016-10-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多