通过强参数获取嵌套属性

时间:2017-01-31 13:26:01

标签: ruby-on-rails

我有一个模型Actes和一个相互关联的模型关键字。

将新的acte表单发送到服务器时,我无法通过强参数。

这是// --------- Mars Rover Kata: Iteration #1 ------------ \\ // ------------------- User Experience using Console --------- \\ var promptSay = "Play with the Console!\nW = UP \nS = DOWN \nD = RIGHT \nA = LEFT"; var gridEdge = "You can\'go there! \n\nPlease notice that you are playing with an imaginary grid and the farest you can go is 9 steps"; var wrongInput = "---WRONG INPUT!--- Please use a correct input i. e. : \nW = UP \nS = DOWN \nD = RIGHT \nA = LEFT"; // Object Definition: (Vars inside a Var , may have functions) \\ var AJRover = { position : [0, 0], invalidInput: function(notright) { // Notification for invalid Input alert(notright); this.move(prompt(wrongInput)); }, invalidKey: function(message) { // Notification if you reach grid's edge alert(message); this.move(prompt(promptSay)); }, move: function(moveRover) { //Directions switch(moveRover.toLowerCase()) { case 'w': this.goDirection("up"); break; case 's': this.goDirection("down"); break; case 'd': this.goDirection("right"); break; case 'a': this.goDirection('left'); break; default: this.invalidInput(wrongInput); } }, goDirection: function(direction) { //Directions Functions switch(direction) { case 'up': if (this.position[1] >= -9 && (this.position[1] + 1) <= 9) { this.position[1]++; break; } else { this.invalidKey(gridEdge); break; } case 'down': if (this.position[1] <= 9 && (this.position[1] -1 ) >= -9) { // this needs to go back and stop at -9 this.position[1]--; break; } else { this.invalidKey(gridEdge); break; } case 'right': if (this.position[0] >= -9 && (this.position[0] + 1) <= 9) { this.position[0]++; break; } else { this.invalidKey(gridEdge); break; } case 'left': if (this.position[0] <= 9 && (this.position[0] -1) >= -9) { this.position[0]--; break; } else { this.invalidKey(gridEdge); break; } } } }; // ---- object END ----- \\\ // 1- This function calls the object move (this.move) // 2- Sends the alert to prompts the var promptSay // 3- Expects input to decide the output while (true) { //This code block allows user move the rover on mars by interacting with console var entry = prompt(promptSay); AJRover.move(entry); console.log('You are now at position: ', AJRover.position); }

params[:acte]

模型acte.rb包含:

"acte"=>{
         "biblio_id"=>"1", 
         "acte_numero"=>"12", 
         "keywords_attributes"=>{"0"=>{"keyword"=>"attestation, "}},
         "texte"=>"<p>test</p>", 
         "complement"=>""
        }

注意:如果我将has_and_belongs_to_many :keywords belongs_to :user, optional: true belongs_to :biblio, optional: true belongs_to :archive, optional: true has_many :comments accepts_nested_attributes_for :keywords, allow_destroy: true, 添加到reject_if: :all_blank,则表单根本不会被发送。

这是在acte_controller中:

accepts_nested_attributes_for

这会引发def params_acte params.require(:acte).permit(:biblio_id, :acte_numero, :keywords_attributes =>[:keyword], : resume, # cut for brievity

1 个答案:

答案 0 :(得分:2)

您最后应添加keywords_attributes

def params_acte
    params.require(:acte).permit(:biblio_id, 
                                 :acte_numero, 
                                 :resume,
                                 # All other model attributes
                                 :keywords_attributes =>[:keyword],

这样可行。

  

注意:始终在结尾处添加嵌套属性

相关问题