FormulairesController #create中的ActiveModel :: ForbiddenAttributesError

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了FormulairesController #create中的ActiveModel :: ForbiddenAttributesError相关的知识,希望对你有一定的参考价值。

我按照本教程了解嵌套表单(http://railscasts.com/episodes/196-nested-model-form-part-1?autoplay=true)。我必须创建表单/问题/答案。它可以用于表单和问题但是当我想在我的数据库中插入答案时它不起作用。

我尝试了两种创建方法:

def create
  @formulaire = current_user.formulaires.new(formulaire_params)
  if @formulaire.save    
    redirect_to @formulaire, notice: "Votre formulaire a bien été crée"   
  else     
    render :new
   end
 end

和#@ form = Form.new(params [:form])

我的formulaire_params方法:

    def formulaire_params
      params.require(:formulaire).permit(:name, :description,       questions_attributes: [:id,:nom, :typequestion,
      answer_attributes:[:id, :content]]) if params[:formulaire]
end

在我看来,我有一个带有fields_for的表单:带有text_field内容的答案,但我的数据库中没有任何内容。

谢谢

答案
 class Formulaire < ActiveRecord::Base

belongs_to :user
has_many :questions, dependent: :destroy
accepts_nested_attributes_for :questions, :reject_if => lambda { |a|       a[:nom].blank? }, :allow_destroy => true


 validates :name, presence: true
 validates :description, presence: true

end

class Question < ActiveRecord::Base
belongs_to :formulaire
has_many :answers, :dependent => :destroy
 accepts_nested_attributes_for :answers, :allow_destroy => true

 #self.inheritance_column = nil
enum typequestion: [:reponse_courte, :choix_multiple, :choix_simple]

end

当我创建提交表单时的日志:

Started GET "/formulaires/112" for 88.172.93.170 at 2017-12-17 19:49:26     +0000
 Cannot render console from 88.172.93.170! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
 Processing by FormulairesController#show as html
  Parameters: {"id"=>"112"}
  Formulaire Load (0.2ms)  SELECT  "formulaires".* FROM "formulaires" WHERE      "formulaires"."id" = ? LIMIT 1  [["id", 112]]
  Question Load (0.2ms)  SELECT "questions".* FROM "questions" WHERE   "questions"."formulaire_id" = ?  [["formulaire_id", 112]]
  Answer Load (0.1ms)  SELECT "answers".* FROM "answers" WHERE  "answers"."question_id" = ?  [["question_id", 153]]
   Answer Load (0.1ms)  SELECT "answers".* FROM "answers" WHERE "answers"."question_id" = ?  [["question_id", 154]]
  Rendered formulaires/show.html.erb within layouts/application (3.3ms)
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 3]]
  Rendered layouts/_navigation.html.erb (3.0ms)
 Rendered shared/_message.html.erb (0.2ms)
  Rendered layouts/_footer.html.erb (0.2ms)
  Completed 200 OK in 82ms (Views: 79.9ms | ActiveRecord: 0.6ms)
另一答案

当我创建一个新表单

  Started GET "/formulaires/new" for 88.172.93.170 at 2017-12-18 09:30:28  +0000
   Cannot render console from 88.172.93.170! Allowed networks: 127.0.0.1,    ::1, 127.0.0.0/127.255.255.255
  Processing by FormulairesController#new as HTML
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 3]]
  Rendered formulaires/_form.html.erb (29.2ms)
  Rendered formulaires/new.html.erb within layouts/application (30.6ms)
  Rendered layouts/_navigation.html.erb (1.2ms)
  Rendered shared/_message.html.erb (0.1ms)
 Rendered layouts/_footer.html.erb (0.0ms)
Completed 200 OK in 82ms (Views: 75.4ms | ActiveRecord: 0.3ms)

当我提交问题和答案时:

Started POST "/formulaires" for 88.172.93.170 at 2017-12-18 09:33:30 +0000
 Cannot render console from 88.172.93.170! Allowed networks: 127.0.0.1,  ::1, 127.0.0.0/127.255.255.255
 Processing by FormulairesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"YAmDvlvqExWqEKyFUXk+9fEOtFyKw/KJL8fbjfDqOox3w0JqjWRYhf  bd4oWSwJXxfVEzuEFsIWOr+vJrF+GoSQ==", "formulaire"=>{"name"=>"aaaaaa", "description"=>"aaaa", "questions_attributes"=>{"0"=>{"nom"=>"aaa",  "typequestion"=>"reponse_courte", "_destroy"=>"0"}, "1"=>{"nom"=>"aaa", "typequestion"=>"reponse_courte", "_destroy"=>"0"}}, "answers"=>{"content"=>"aaaa"}}, "commit"=>"Valider le formulaire"}
   User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 3]]
 Unpermitted parameter: _destroy
 Unpermitted parameter: _destroy
 Unpermitted parameter: answers
(0.1ms)  begin transaction
SQL (0.4ms)  INSERT INTO "formulaires" ("name", "description", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)  [["name", "aaaaaa"], ["description", "aaaa"], ["user_id", 3], ["created_at", "2017-12-18 09:33:30.509450"], ["updated_at", "2017-12-18 09:33:30.509450"]]
   SQL (0.1ms)  INSERT INTO "questions" ("nom", "typequestion", "formulaire_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)  [["nom", "aaa"], ["typequestion", 0], ["formulaire_id", 114], ["created_at", "2017-12-18 09:33:30.511078"], ["updated_at", "2017-12-18 09:33:30.511078"]]
  SQL (0.1ms)  INSERT INTO "questions" ("nom", "typequestion", "formulaire_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)  [["nom", "aaa"], ["typequestion", 0], ["formulaire_id", 114], ["created_at", "2017-12-18 09:33:30.512317"], ["updated_at", "2017-12-18 09:33:30.512317"]]
   (9.0ms)  commit transaction
   Redirected to https://projetx-hakimghanem.c9users.io/formulaires/114
  Completed 302 Found in 24ms (ActiveRecord: 10.0ms)


  Started GET "/formulaires/114" for 88.172.93.170 at 2017-12-18 09:33:31 +0000
   Cannot render console from 88.172.93.170! Allowed networks: 127.0.0.1,   ::1, 127.0.0.0/127.255.255.255
  Processing by FormulairesController#show as HTML
   Parameters: {"id"=>"114"}
   Formulaire Load (0.2ms)  SELECT  "formulaires".* FROM "formulaires" WHERE "formulaires"."id" = ? LIMIT 1  [["id", 114]]
   Question Load (0.1ms)  SELECT "questions".* FROM "questions" WHERE    "questions"."formulaire_id" = ?  [["formulaire_id", 114]]
  Answer Load (0.1ms)  SELECT "answers".* FROM "answers" WHERE "answers"."question_id" = ?  [["question_id", 156]]
  Answer Load (0.1ms)  SELECT "answers".* FROM "answers" WHERE "answers"."question_id" = ?  [["question_id", 157]]
  Rendered formulaires/show.html.erb within layouts/application (2.0ms)
 User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?   ORDER BY "users"."id" ASC LIMIT 1  [["id", 3]] 
 Rendered layouts/_navigation.html.erb (2.2ms)
  Rendered shared/_message.html.erb (0.1ms)
  Rendered layouts/_footer.html.erb (0.0ms)
 Completed 200 OK in 54ms (Views: 52.2ms | ActiveRecord: 0.5ms)

以上是关于FormulairesController #create中的ActiveModel :: ForbiddenAttributesError的主要内容,如果未能解决你的问题,请参考以下文章

CR, LF, CR/LF 回车 换行

CR, LF, CR/LF 回车 换行

CR LF CR/LF

宝,我今天CR了,C的什么R? 走过场的CR

ASCII中LF与CR区别?

022cr18nl14mo3是啥材料