如何在Ruby中定义此方法?

时间:2015-08-18 23:20:43

标签: ruby-on-rails ruby heroku

我真的坚持这个错误,希望有人帮助我解决它。 所以,我使用这个助手:

module BooksHelper
  def image_from_amazon(amazon_id)
        image_tag "http://images.amazon.com/images/P/#{amazon_id}.01.ZTZZZZZZ.jpg"
  end
end

查看档案

> <%= image_from_amazon(book.amazon_id) %>

在我的本地机器上一切正常,除了Heroku。日志:

> ActionView::Template::Error (undefined method `amazon_id' for #<Book:0x007f06994ea498>):
2015-08-18T16:28:19.320038+00:00 app[web.1]:      7: <% @books.each do |book| %>
2015-08-18T16:28:19.320040+00:00 app[web.1]:      8: <div class="book">
2015-08-18T16:28:19.320041+00:00 app[web.1]:      9:   <div class="cover">
2015-08-18T16:28:19.320042+00:00 app[web.1]:     10:     <%= image_from_amazon(book.amazon_id) %>
2015-08-18T16:28:19.320044+00:00 app[web.1]:     11:   </div>
2015-08-18T16:28:19.320045+00:00 app[web.1]:     12:   <div class="content">
2015-08-18T16:28:19.320047+00:00 app[web.1]:     13:     <h4 class="title"><%= link_to book.title, book %></h4>
2015-08-18T16:28:19.320048+00:00 app[web.1]:   app/views/books/index.html.erb:10:in `block in _app_views_books_index_html_erb__843408363563836431_69833158224880'
2015-08-18T16:28:19.320050+00:00 app[web.1]:   app/views/books/index.html.erb:7:in `_app_views_books_index_html_erb__843408363563836431_69833158224880'

1 个答案:

答案 0 :(得分:2)

这一行是关键:

> ActionView::Template::Error (undefined method `amazon_id' for #<Book:0x007f06994ea498>):

您的book实例未响应amazon_id。尝试检查值,例如

puts "amazon_id: #{book.try(:amazon_id)}"