Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Can';是否指定嵌套属性?_Ruby On Rails - Fatal编程技术网

Ruby on rails Can';是否指定嵌套属性?

Ruby on rails Can';是否指定嵌套属性?,ruby-on-rails,Ruby On Rails,我试图分配属于答案的注释(答案有很多注释)并得到一个不能分配大规模保护属性的答案 这是我的评论控制器 class CommentsController < ApplicationController def create @answer = Answer.find(params[:answer_id]) @comment = @answer.comments.new(params[:comment]) @comment.save redirect_to question_

我试图分配属于答案的注释(答案有很多注释)并得到一个不能分配大规模保护属性的答案

这是我的评论控制器

class CommentsController < ApplicationController
 def create
  @answer = Answer.find(params[:answer_id])
  @comment = @answer.comments.new(params[:comment])
  @comment.save
  redirect_to question_path(@answer)
 end
我的答案模型接受注释的嵌套属性

这是我的表格

<%= form_for([@answer, @comment]) do |f| %>
  <p>
  </p>
  <p>
    <%= f.label :comment %>
    <%= f.text_area :answer, :cols => "50", :rows => "30"%>
  </p>
  <p>
    <%= f.submit "Submit Comment" %>
  </p>


“50”,:行=>“30”%>


有什么想法吗?

在评论控制器中使用参数之前,您应该允许使用参数,在rails4中,您可以这样做

class CommentsController < ApplicationController
    def create
        @answer = Answer.find(params[:answer_id])
        @comment = @answer.comments.new(params.require(:comment).permit(:answer))
        @comment.save
        redirect_to question_path(@answer)
    end
end
然后在控制器中使用此函数创建新记录,如下所示:

@comment = @answer.comments.new(comment_params)
@comment.save
正如你所说的,你在答案中为注释嵌套了属性,比如

class Answer < ActiveRecord::Base
   has_many :comments, dependent: :destroy
   accepts_nested_attributes_for :comments
end
class-Answer
并且u也使用attr_accessible,以便允许参数以嵌套形式出现


attr\u accessible:comments\u attributes
以及答案中的答案attrs。rb

我允许答案接受注释中的嵌套属性(属于答案),那么这是一个什么问题呢?注释中的所有字段都可以访问,因此我不确定许可证为什么会有帮助,甚至不知道为什么我不能分配这些字段。好吧,那么您使用的是rails版本<4或gem嵌套属性…哪一个。。?让我更新我的答案。它实际上是受保护的,上面的评论中有打字错误。对不起。Rails 3,很抱歉没有包括这个!同样的结构适用于我的问题-->答案,但不适用于答案-->评论。是的,添加了attr_accessible,这似乎很难理解。也许我会创建新的模型,看看会发生什么。
@comment = @answer.comments.new(comment_params)
@comment.save
class Answer < ActiveRecord::Base
   has_many :comments, dependent: :destroy
   accepts_nested_attributes_for :comments
end