Question: undefined method `request' for true:TrueClass

Question

undefined method `request' for true:TrueClass

Answers 1
Added at 2016-12-19 00:12
Tags
Question

I have an app structure with nested routes in which a proposal belongs to a request and a request has many proposals.

When I execute a send_proposal method, I am trying to get it to update the status of the request to which that proposal belongs, but I am getting an error that says undefined method 'request' for true:TrueClass.

My route for this method (not that I think it matters) is:

put "proposal/:id/send_proposal" => "proposals#send_proposal", as: "send_proposal"

Here is my send_proposal method, as found in my 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

I have looked at many SO posts for other TrueClass errors, but can't seem to find one with a problem like this. Can anyone see what I'm doing wrong or help me conceptualize what TrueClass errors are generally about?

Answers
nr: #1 dodano: 2016-12-19 01:12

update_attributes is an alias for update:

update(attributes)
Updates the attributes of the model from the passed-in hash and saves the record, all wrapped in a transaction. If the object is invalid, the saving will fail and false will be returned.

and update returns true or false (the documentation could be a lot more explicit about this) not the updated model instance. So this:

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

will leave @proposal as true or false and neither of those have an update_attributes method.

Your controller method should look more like this:

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

You probably want to do some error checking on those two update calls too.

Source Show
◀ Wstecz