未定义的方法`request'为true:TrueClass

时间:2016-12-18 23:57:05

标签: ruby-on-rails activerecord rails-activerecord

我有一个包含嵌套路线的应用结构,其中proposal属于requestrequest有许多proposals

当我执行send_proposal方法时,我试图让它更新status所属request的{​​{1}},但我得到的是表示proposal的错误。

这种方法的路线(不是我认为重要)是:

undefined method 'request' for true:TrueClass

以下是我put "proposal/:id/send_proposal" => "proposals#send_proposal", as: "send_proposal" 中的send_proposal方法:

proposals_controller

我查看过很多其他 def send_proposal @proposal = Proposal.find(params[:id]) ProposalMailer.send_proposal_to_client(@proposal, @proposal.request.user).deliver_now @proposal = @proposal.update_attributes(status: "Sent to Client") @proposal.request = @proposal.request.update_attributes(archived: "Proposal Sent to Client") <<<<<<<<<ERROR CALLED ON THIS LINE flash[:notice] = "Your proposal has been sent to the client!" end 错误的SO帖子,但似乎找不到像这样的问题。任何人都可以看到我做错了什么或帮助我概念化TrueClass错误通常是什么?

1 个答案:

答案 0 :(得分:2)

update_attributesupdate的别名:

  

<强>更新(属性)
  从传入的哈希更新模型的属性并保存记录,所有记录都包含在事务中。如果对象无效,则保存将失败并返回false

update返回truefalse(文档可能更加明确),而不是更新的模型实例。所以这个:

@proposal = @proposal.update_attributes(status: "Sent to Client")

@proposal保留为truefalse,而且这两种方法都没有update_attributes方法。

您的控制器方法看起来应该更像这样:

def send_proposal
  #...
  @proposal.update(status: "Sent to Client"))
  @proposal.request.update(archived: "Proposal Sent to Client")
  #...
end

您可能也希望对这两个update来电进行错误检查。