ActionView :: Template ::错误:未定义的方法`最新&#39; for#<class:0x007f8b5d07e5c8> </class:0x007f8b5d07e5c8>

时间:2014-07-25 08:32:23

标签: ruby-on-rails methods indexing undefined actionview

我有一个错误,我无法解决,我明白'get:index'由于某种原因没有链接。

StoreControllerTest#test_should_get_index:
ActionView::Template::Error: undefined method latest' for #<Class:0x007f8b5d07e5c8> app/views/store/index.html.erb:7:in _app_views_store_index_html_erb___4260644534451553693_70118269675460'
    test/controllers/store_controller_test.rb:5:in `block in <class:StoreControllerTest>

以下是store_controller_test.rb的代码

require 'test_helper'

class StoreControllerTest < ActionController::TestCase
  test "should get index" do
    get :index
    assert_response :success
    assert_select '#columns #side a', minimum: 4
    assert_select '#main .entry', 3
    assert_select 'h3', 'Programming Ruby 1.9'
    assert_select '.price', /\$[,\d]+\.\d\d/
  end

end

这是index.html.erb的代码

<% if notice %>
<p id='notice'> <%= notice %> </p>
<% end %>

<h1>Your Pragmatic Catalog</h1>

<% cache ['store', Product.latest] do %>
<% @products.each do |product| %> 
<% cache ['entry', product] do %>
<div class='entry'>
<%= image_tag(product.image_url) %>
<h3><%= product.title %></h3>
<%= sanitize(product.description) %>
<div class='price_line'>
<span class='price'><%= number_to_currency(product.price) %></span>
<%= button_to 'Add to Cart', line_items_path(product_id: product) %>
  </div>
 </div>
  <% end %>
<% end %>
<% end %>

2 个答案:

答案 0 :(得分:4)

没有为latest类定义Product方法。你可能意味着:

Product.last

答案 1 :(得分:0)

迟到的答案,但我刚碰到这个。您应该在app / models / product.rb

中使用以下方法
def self.latest
  Product.order(:updated_at).last
end

这是它引用最新的&#39;来自(使用Rails 4的敏捷Web开发中的页面104)