简体   繁体   中英

How to define this method in Ruby?

I really stuck with this error and hope someone help me to solve it. So, I use this helper:

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

View file

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

On my local machine everything working fine except Heroku. Logs:

> 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'

This line is the key:

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

Your book instance doesn't respond to amazon_id . Try checking the value, eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM