简体   繁体   English

在Rails 4中未设置实例变量

[英]Instance variable not being set in Rails 4

I'm having a problem with a Rails 4 app that used to work perfectly fine. 我曾经使用过的Rails 4应用程序存在问题。 Now, an instance variable remains nil after being set. 现在,实例变量在设置后仍然为零。 In the controller, I have the following code: 在控制器中,我有以下代码:

def create
  @reportdate = Reportdate.new(reportdate_params)
  weekly_summary = getSOData(@reportdate.start_date, @reportdate.end_date)
  @reportdate.questions = weekly_summary[:question_ids]
  @reportdate.num_answers = weekly_summary[:answers]
  @reportdate.views = weekly_summary[:views]
  respond_to do |format|
  if @reportdate.save
  ...

When I run this, @reportdate.num_answers and @reportdate.views gets set, but @reportdate.questions remains nil . 当我运行此命令时,会设置@reportdate.num_answers@reportdate.views ,但是@reportdate.questions仍然为nil

When I run it in the debugger, I verified that weekly_summary[:question_ids] equals 当我在调试器中运行它时,我验证了weekly_summary[:question_ids]等于

[21636454, 21539706, 21611661, 21632781, 21583630, 21614059, 21634704, 21615609, 21595829, 21564079, 21587196, 21537280] . [21636454, 21539706, 21611661, 21632781, 21583630, 21614059, 21634704, 21615609, 21595829, 21564079, 21587196, 21537280]

This used to work before and I have no idea why it's not working now. 这以前曾经起作用,我不知道为什么现在不起作用。

def create
  @reportdate = Reportdate.new(reportdate_params)
  weekly_summary = getSOData(@reportdate.start_date, @reportdate.end_date)

  @reportdate.questions   = weekly_summary[:question_ids]
  @reportdate.num_answers = weekly_summary[:answers]
  @reportdate.views       = weekly_summary[:views]

  respond_to do |format|
      if @reportdate.save
         ...

I think the answer is outside the scope of your controller. 我认为答案超出了您的控制器范围。 I changed the above to reflect the data used in the action, and as you can see, the main ways you receive data are from the weekly_summary method 我更改了上面的内容以反映该操作中使用的数据,并且如您所见,接收数据的主要方法是weekly_summary方法

I would check these: 我会检查这些:

  1. Is weekly_summary populating the variable correctly? weekly_summary是否正确填充了变量?
  2. Is .questions an attribute of the Reportdate object? .questionsReportdate对象的属性吗? If not, do you have an attr_accessor ? 如果没有,您有attr_accessor吗?
  3. Your Reportdate object needs to be CamelCase - should be ReportDate (this could cause an issue) 您的Reportdate对象必须为CamelCase应为ReportDate (这可能会引起问题)
  4. As mentioned in the comments, your model may prevent saving due to validation issues. 如评论中所述,由于验证问题,您的模型可能会阻止保存。

All this points to some other part of the app having issues 所有这些都表明该应用程序存在其他问题

Could you post your Reportdate model and weekly_summary code please? 您能张贴您的Reportdate模型和weekly_summary代码吗?

Thank you all for responding. 谢谢大家的回应。 The problem was that I was setting the instance variable incorrectly. 问题是我没有正确设置实例变量。 I didn't think I made a change in that part of the code, but it was impossible to work the way it was written, so I must have. 我认为我没有对代码的那部分进行更改,但是不可能按照编写的方式工作,所以我必须这样做。 It is all fixed and working now. 现在已经固定并可以工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM