I have never seen this error message before
undefined method `new' for Book:Module
and was wondering if anyone knew why i am getting it, my first thought is naming conventions but am unsure
I have a module
module Book::BookFinder
BOOK_URL = 'https://itunes.apple.com/lookup?isbn='
def book_search(search)
response = HTTParty.get(BOOK_URL + "#{search}", :headers => { 'Content-Type' => 'application/json', 'Accept' => 'application/json' })
results = JSON.parse(response.body)["results"]
end
end
and then include it within my controller
class BookController < ApplicationController
before_filter :authenticate_admin_user!
include Book::BookFinder
def results
results = book_search(params[:search])
@results = results
@book = Book.new
@book.author = results[0]["artistName"]
end
def create
@book = Book.new(params[:book])
if @book.save
redirect_to @book, notice: 'Book was successfully saved'
else
render action:new
end
end
end
has anyone come across this before and know how to fix it?
Thanks
The issue comes from your use of
Book.new.Book is a Module (Book::BookFinder) and that can’t be instanced.
The Methods inside Book::BookFinder are now mixed into your controller and any methods in Book::BookFinder can be called directly on the controller without having to instance Book::BookFinder.
If you really want this to be a object use
classinstead of Module.Update:
You obviously can’t just replace module with class.
But if you want to have a object Book that you can new and set properties on you need to write a class for that (and remove the
include Book::BookFinderyou now used for the Module.A sample calss would look like this:
Hope this helps, but I strongly suggest you check out some tutorials on Ruby classes and how these things work.