0

Rails オブジェクトのコントローラーからヘルパー メソッドを呼び出そうとしましたが、引き続きこのエラーが発生します。ここにすべてのコードがあります。

class AuctionsController < ApplicationController
  helper_method :bid

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

見る

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

スタックトレース

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)

私のコードが間違っているのか、方法論が間違っているのか、誰か教えてもらえますか? ありがとう

編集してください、以下の私の答えを見てください、それは私の本当の問題です...

4

2 に答える 2

2

コントローラーのメソッドについて誤解しています。オブジェクトのコントローラーメソッドを呼び出そうとしていますが、それはできません。AuctionsController のメソッドはコントローラの一部であり、クラスの一部ではありません。操作をモデル クラスに追加する場合は、それらをオークション モデルに記述する必要があります。

@auction をパラメーターとして渡して、コントローラーへの正しい呼び出し

<%= link_to("Bid", @auction )%>
于 2014-11-11T12:01:33.617 に答える