Ruby on rails ActiveModel::ForbiddenAttribute错误(ActiveModel::ForbiddenAttribute错误)

Ruby on rails ActiveModel::ForbiddenAttribute错误(ActiveModel::ForbiddenAttribute错误),ruby-on-rails,angular4-forms,Ruby On Rails,Angular4 Forms,我在这方面遇到了很多问题,但我无法从中找到解决办法。 客户\u controller.rb如下所示: def create @customers = Customer.new(params) respond_to do |format| if @customers.save format.json { render json: @customers.to_json } else format.json { render json: @customers.errors,

我在这方面遇到了很多问题,但我无法从中找到解决办法。 客户\u controller.rb如下所示:

def create
@customers = Customer.new(params)
respond_to do |format|
  if @customers.save
    format.json { render json: @customers.to_json }
  else
    format.json { render json: @customers.errors,
      status: :unprocessable_entity }
  end
end
end

class Customer < ActiveRecord::Base
end
class Customer < ActiveRecord::Base
end

params = ActionController::Parameters.new({
  customer: {
    first_name: "Godson",
    last_name:  "Chukwu",
    username:   "Son",
    email:      "godson@son.org"
 }
})
permitted = params.require(:customer).permit(:first_name, :last_name,                  :username, :email)
permitted
permitted.permitted?
Customer.first.update!(permitted)
class ApplicationController < ActionController::Base
  protect_from_forgery with: :null_session
end
firefox浏览器中的网络预览请求参数如下:

def create
@customers = Customer.new(params)
respond_to do |format|
  if @customers.save
    format.json { render json: @customers.to_json }
  else
    format.json { render json: @customers.errors,
      status: :unprocessable_entity }
  end
end
end

class Customer < ActiveRecord::Base
end
class Customer < ActiveRecord::Base
end

params = ActionController::Parameters.new({
  customer: {
    first_name: "Godson",
    last_name:  "Chukwu",
    username:   "Son",
    email:      "godson@son.org"
 }
})
permitted = params.require(:customer).permit(:first_name, :last_name,                  :username, :email)
permitted
permitted.permitted?
Customer.first.update!(permitted)
class ApplicationController < ActionController::Base
  protect_from_forgery with: :null_session
end

我哪里弄错了?

因为您是从“Angular”提交的,所以您没有传递跨站点请求伪造(CSRF)令牌

您可以通过多种方式解决此问题,但有两种选择

在应用程序中_controller.rb添加

skip_before_action :verify_authenticity_token
如果您只需要跳过创建的跨站点请求伪造(CSRF),您可以这样做


以一种更好的方式(不会避免出于正当理由创建的安全检查),您可以在表单中包含身份验证令牌(helper
form\u authenticity\u token
返回有效令牌)。谢谢@MrYoshiji,我已经添加了
protect\u from\u fackery with::null\u session
,application\u controller.rb。现在,当我在customers\u controller.rb中添加一个私有customer\u params方法作为参数来创建方法时,我得到了以下错误:
ActionController::UnknownFormat(ActionController::UnknownFormat):
。同时,表单现在正在向数据库提交数据。但问题似乎来自customers_controller.rb create方法中的
respond_to do | format |
。我将如何解决这个问题?谢谢。这是我的控制台日志:
19:45:55 rails.1 |参数:{“first_name”=>“Newman”,“last_name”=>“Kalu”,“username”=>“Didi”,“email”=>“new@newer.ng“,”客户“=>{”名字“=>”纽曼“,”姓氏“=>”卡鲁“,”电子邮件“=>”new@newer.ng,“username”=>“Didi”}19:45:55 rails.1 |未经允许的参数::customer 19:45:55 rails.1 | SQL(13.2ms)插入“客户”(17.1ms)提交19:45:55 rails。1 |在35毫秒内完成406不可接受(ActiveRecord:30.6ms)19:45:55 rails。1 | ActionController::UnknownFormat(ActionController::UnknownFormat):
跳过了一些输出。我应该第一次看到这一点,但您传递的是
{“first\u name”=>“Newman”,“姓氏”=>“卡鲁”,“用户名”=>“滴滴”,“电子邮件”=>”new@newer.ng“,”客户“=>{”名字“=>”纽曼“,”姓氏“=>”卡鲁“,”电子邮件“=>”new@newer.ng“,“username”=>“Didi”}
。在发布这些参数之前,您需要将其包装在
packs/customers.js
中的
customer
中。
var body=JSON.stringify(customer)
应该是
var body=JSON.stringify({customer:customer});
@nzajt,当我把它修改成
var body=JSON.stringigy({customer:customer})
参数:{“customer”=>{“first_name”=>“wiss”,“last_name”=>“Biggs”,“username”=>“Wiz”,“email”=>“wiz@wiz.com"}}未经允许的参数::customer 02:11:37 rails.1 | SQL(1.0ms)插入“customers”…(0.2ms)回滚在4ms内完成500个内部服务器错误(ActiveRecord:1.5ms)ActiveRecord::NotNullViolation(PG::NotNullViolation:错误:列“first_name”中的null值“违反了非空约束
跳过了某些输出。您应该包括一个名为“Authentity\u token”的隐藏字段和(Rails helper)
form\u Authentity\u token设置的值
skip_before_action :verify_authenticity_token, only: :create