NoMethodError, undefined method 'bid'

193 views Asked by At

Ive been trying to call a helper method from my controller on a rails object and i continue to get this error. Here is all my code.

class AuctionsController < ApplicationController
  helper_method :bid

  def bid
    @auction = @auction.update_attribute(:current_price == :current_price + 1.00)
  end

view

<%= link_to("Bid", @auction.bid(auction) )%>

stack trace

Started GET "/auctions" for 127.0.0.1 at 2014-11-11 05:46:16 -0600
Processing by AuctionsController#index as HTML
  Auction Load (1.7ms)  SELECT "auctions".* FROM "auctions"
  Rendered auctions/index.html.erb within layouts/spacelab (199.7ms)
Completed 500 Internal Server Error in 234ms

ActionView::Template::Error (undefined method `bid' for nil:NilClass):
    26:     <h3, class="textcolor"><%= auction.description %></h3><br />
    27:     <h3, class="textcolor"><%= auction.start_time.strftime("Opens on %B %d on %I:%M %p") %></h3><br />
28:     <h3, class="textcolor"><%= auction.end_time.strftime("Closes on %B %d on %I:%M %p") %></h3><br />
29:     <%= link_to("Bid", @auction.bid(auction) )%>
30: 
31:         <%= link_to 'Show', auction, class: "btn btn-primary btn-lg btn-block" %>
32:         
  app/views/auctions/index.html.erb:29:in `block in _02d262c45abda05ea87ddc9c2c9ec185'
  app/views/auctions/index.html.erb:16:in `_02d262c45abda05ea87ddc9c2c9ec185'


      Rendered /Users/claymccullough/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.0ms)
  Rendered /Users/claymccullough/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.8ms)
  Rendered /Users/claymccullough/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout (92.3ms)

Could anyone tell me if my code is wrong or methodology is incorrect? thanks

edit please see my answer below, that is my real problem...

2

There are 2 answers

0
ppascualv On BEST ANSWER

You have a missundertanding about methods on the controller, you're trying to call a Controller Method on an object, you can't do that. The methods on AuctionsController are part of Controllers no part of the Class, if you want to add operations to a Model class you have to write them in Auction Model

Correct call to your Controller, passing @auction as a parameter

<%= link_to("Bid", @auction )%>
0
sebkkom On

First of all, the error message ActionView::Template::Error (undefined method bid for nil:NilClass) means that you are trying to call a method (bid) on an object that doesn't exist (@auction). Furthermore, the @auction.bid(auction) bit doesn't look good to me either - from a semantics and code-reading point of view but I don't know what you are trying to do exactly.

If you show us the rest of your AuctionsController we will be able to tell you more about what's wrong.