fegloff
fegloff

Reputation: 51

grails question (sample 1 of Grails To Action book) problem with Controller and Service

I'm doing Grails To Action sample for chapter one. Every was just fine until I started to work with Services. When I run the app I have the following error:

groovy.lang.MissingPropertyException: No such property: quoteService for class: qotd.QuoteController

at qotd.QuoteController$_closure3.doCall(QuoteController.groovy:14)

at qotd.QuoteController$_closure3.doCall(QuoteController.groovy)

at java.lang.Thread.run(Thread.java:619)

Here is my groovie QuoteService class, which has an error within the definition of GetStaticQuote (ERROR: Groovy:unable to resolve class Quote)

 package qotd

    class QuoteService {

 boolean transactional = false

 def getRandomQuote() {
  def allQuotes = Quote.list()
  def randomQuote = null
  if (allQuotes.size() > 0) {
   def randomIdx = new Random().nextInt(allQuotes.size())
   randomQuote = allQuotes[randomIdx]
  } else {
   randomQuote = getStaticQuote()
  }
  return randomQuote
 }

 def getStaticQuote() {
  return new Quote(author: "Anonymous",content: "Real Programmers Don't eat quiche")
 }
     }

Eclipse show me an error flag on the definition of getStaticQuote:

ERROR: Groovy:unable to resolve class Quote

Any Clues?

Controller groovie class

package qotd

class QuoteController {

   def index = {
     redirect(action: random)
   }

   def home = {
     render "<h1>Real Programmers do not each quiche!</h1>" 
   }

  def random = {
     def randomQuote = quoteService.getRandomQuote()
     [ quote : randomQuote ]
   }

  def ajaxRandom = {
     def randomQuote = quoteService.getRandomQuote()
     render "<q>${randomQuote.content}</q>" +
     "<p>${randomQuote.author}</p>"
   }
}

Quote Class:

package qotd

class Quote {
   String content
   String author
   Date created = new Date()

   static constraints = {
     author(blank:false)
     content(maxSize:1000, blank:false) 
    }
}

I'm doing the samples using STS. Any advice?

Regards,

Francisco

Upvotes: 1

Views: 1150

Answers (3)

hvgotcodes
hvgotcodes

Reputation: 120168

do

def quoteService

at the top of your controller and it will be injected into the controller automatically

Upvotes: 3

Muhammad Shahab
Muhammad Shahab

Reputation: 4270

I did

def quoteService = new QuoteService()

and it solved my problem

Upvotes: 0

Woot4Moo
Woot4Moo

Reputation: 24316

groovy.lang.MissingPropertyException: No such property: quoteService for class: qotd.QuoteController

I dont code in grails but it appears as though you need to declare quoteService somewhere in the controller.

Upvotes: 0

Related Questions