Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.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 创建新调查时出错,因为关联的模型无效(问题)_Ruby On Rails - Fatal编程技术网

Ruby on rails 创建新调查时出错,因为关联的模型无效(问题)

Ruby on rails 创建新调查时出错,因为关联的模型无效(问题),ruby-on-rails,Ruby On Rails,我有一个调查作为我正在构建的应用程序的一部分。用户可以创建调查并动态指定问题(可以有任意数量的问题),因此我使用了一个关联模型: #survey.rb has_many :survey_questions, :dependent => :destroy has_many :survey_answers, :dependent => :destroy after_update :save_survey_questions validates_associated

我有一个调查作为我正在构建的应用程序的一部分。用户可以创建调查并动态指定问题(可以有任意数量的问题),因此我使用了一个关联模型:

  #survey.rb
  has_many :survey_questions, :dependent => :destroy
  has_many :survey_answers, :dependent => :destroy

  after_update :save_survey_questions
  validates_associated :survey_questions

  def save_survey_questions
    survey_questions.each do |t|
      if t.should_destroy?
        t.destroy
      else
        t.save(false)
      end
    end
  end  

  def survey_question_attributes=(survey_question_attributes)
    survey_question_attributes.each do |attributes|
      if attributes[:id].blank?
        survey_questions.build(attributes)
      else
        survey_question = survey_questions.detect { |e| e.id == attributes[:id].to_i }
        survey_question.attributes = attributes
      end
    end
  end

  #surveys_controller.rb

  def new
    @survey = Survey.new
    if(@survey.survey_questions.empty?)
      @survey.survey_questions.build
    end
    respond_to do |format|
      format.html # new.html.erb
    end
  end

  def create
    @survey = Survey.new(params[:survey])
    respond_to do |format|
      if @survey.save
        format.html { redirect_to(survey_path(:id => @survey)) }
      else
        format.html { render :action => "new" }
      end
    end
  end

#survey_question.rb
class SurveyQuestion < ActiveRecord::Base
  belongs_to :survey
  attr_accessor :should_destroy

  def should_destroy?
    should_destroy.to_i == 1
  end

  validates_presence_of :question, :survey_id

end
#survey.rb
有很多:调查问题,:依赖=>:破坏
有很多:调查答案,:依赖=>:破坏
更新后:保存调查问题
验证\u关联:调查\u问题
def保存调查问题
调查每个问题|
如果t.你应该毁灭吗?
t、 毁灭
其他的
t、 保存(错误)
结束
结束
结束
def调查问题属性=(调查问题属性)
调查_问题_属性。每个do |属性|
如果属性[:id]。为空?
调查问题。构建(属性)
其他的
survey_question=survey_questions.detect{e|e.id==属性[:id]。to_i}
调查问题属性=属性
结束
结束
结束
#测量控制器.rb
def新
@survey=survey.new
如果(@survey.survey\u questions.empty?)
@调查。调查问题。构建
结束
回应待办事项|格式|
format.html#new.html.erb
结束
结束
def创建
@测量=测量。新建(参数[:测量])
回应待办事项|格式|
如果@survey.save
format.html{重定向到(测量路径(:id=>@survey))}
其他的
format.html{render:action=>“new”}
结束
结束
结束
#调查问题.rb
类调查问题
问题是,当我提交时,我在问题上出现了一个错误:

@错误={“调查问题”=>[“无效”、“无效”、“无效”]}

我相信这是因为我的调查id没有被填写

有没有办法克服这个问题

如果我创建的调查没有问题,然后通过编辑添加,那么效果会很好。

我很确定这会对你有很大帮助,你会发现一些构建相关对象的例子,这似乎是你的问题(因为调查问题中的调查id没有填写),基本上,您应该在模型中定义如下内容:

class Survey < ActiveRecord::Base has_many :survey_questions accepts_nested_attributes_for :survey_questions, :allow_destroy => true ... end 班级调查true ... 结束
它将通过
调查
处理所有
调查问题
验证

您的代码看起来接近标准-->

您确定正确返回了问题参数吗?我之所以这么问,是因为这是您要验证的另一件事,而且您没有表单代码,所以我看不到控制器返回了什么。

好的

我已经设法修复了它,这对我来说是一个非常愚蠢的错误

在survey_question.rb中,我有一句话:

validates_presence_of :question, :survey_id
然而,rails会自动处理调查id,因为有很多属于关系

所以这应该是
验证是否存在:问题

我还在发现这一点的过程中,将rails升级到了2.3.4,并开始使用:

accepts_nested_attributes_for :survey_questions, :allow_destroy => true

它处理了所有属性等。

您使用的是哪个版本的rails?从2.3.x开始,您可以使用它,这将导致一个更整洁/更为最新的解决方案。我使用的是2.2.2,现在升级到2.3.4,这是否只是一个输入错误,您使用的是“if@event.save”而不是“if@survey.save”?是的,只是一个输入错误,实际上应该是@survey.save:)唯一的问题是我使用的是rails 2.2.2嗨,克里斯,酷,一切都为您服务,但在我看来,您应该使用嵌套的属性,让rails通过包含
:如果
检查
调查问题,则拒绝
,以及
调查问题属性=(调查问题属性)
,来实现
保存调查问题的方法!