I am relatively new to Grails, and want to learn more about how to create some quick mock data while developing my app.
In Ruby on Rails, i can use the console to create new objects, like so:
Book.create{title: "new book", author_id: 2}
And with Rails i can see all like so:
Book.all
What are the grails equivalents for this? I am using Grails 2.0.0
You can use the Grails shell (as Michael alludes to). For example, here I have a test app with a domain class test.Book. In this example, I first load the Grails interactive mode, run the app, then run the shell and test saving and listing a Book entry:
/home/tim/cmdline $ grails
| Enter a script name to run. Use TAB for completion:
grails> run-app
| Compiling 38 source files
| Server running. Browse to http://localhost:8080/cmdline
| Application loaded in interactive mode. Type 'exit' to shutdown.
| Enter a script name to run. Use TAB for completion:
grails> shell
| Packaging Grails application.....
Groovy Shell (1.8.4, JVM: 1.6.0_29)
Type 'help' or '\h' for help.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
groovy:000> new test.Book( title:'Groovy things' ).save()
===> test.Book : 1
groovy:000> test.Book.list()
===> [test.Book : 1]
groovy:000>
If you install the Grails console plugin, and navigate to /console, you can create objects using
new Book(title: "new book").save()
and get a list of all books using
Book.list()
If you are using an in-memory database during development the easiest way to
add testdata is to add code to Bootstrap.groovy that creates and saves
data.
Another option is to use the Grails Console: http://www.grails.org/Command+Line+Tools
Related
it's great news that Grails 3.2.1 now comes with an Angular2 profile, but I don't know how to use it.
The profile description tells me that there should be the standard command like create-domain-class, but when I create an app through
grails create-app test-ng --profile angular2
I get a working angular2 project, but it even seems that this project is not recognized as grails app. When I enter the grails cli, I only get the commands like create-app which are available outside of projects.
What am I doing wrong?
your grails create-app test-ng --profile angular2
command should have created three folders in your test-ng-project-folder:
client
gradle
server
change to server and start grails command
now you should have the wellknown grails project.
but i am still on the first steps of examining the new grails-profile. so i hope i could help you.
Nowadays this layout is called "multi-project". Separate 4 the client and server applications. To make things easier, the tasks test, integrationTest, and bootRun have been created in the client application to make executing those tasks easier across the whole application.
Since Gradle executes tasks synchronously, and the bootRun task will never finish, it is important to execute it in parallel. At the root of the project:
./gradlew bootRun --parallel
Opening things also separately by 2 instances of your IDE or preferred text processor.
see the docs
grails list-profiles
show list of available profiles, I suggest you use this because for example now angular2 profile is angular and angular1 is angularJS.
It's a little strange question, but why when I'm running next command:
grails create-app project.api
Grails 2.5.0 creates folder project.api with main package project.api, but Grails 3.0.1 creates folder api with package project and ignores provided project full name?
Try it like this:
grails create-app "project.api"
Because the dot has special meaning, used to show package hierarchy(my guess).
I am doing sample application from git-hub [book-flow] I created domain classes,controller classes and also views of the book -flow application, then I executed the application in development mode but the application is terminated automatically in console it showed
Loading Grails 2.1.1
| Configuring classpath.
| Environment set to development.....
| Packaging Grails application.....
| Compiling 2 source files.....
| Running Grails application
I unable to find where is the problem and I want to know that where can I find the applications on grails other than github.
Srujan
I thought I give you more of an insight into grails now that I have a little time on my hands...
most of the grails should remember one day not too long ago we were also in the same boat and had no idea where to start...
I would suggest getting hold of a good book such as definitive guide to grails 2 and reading/experimenting....
To create a basic app with one object :
The following is using grails command line - so refer to No console output available on linux when executing grails/groovy for expoerting out java grails - I Guess you are windows so same but on windows environment...
$ grails create-app grails-example
$ cd grails-example
$ grails create-domain-class Books
$ vi grails-app/domain/grails/example/Books.groovy
cat grails-app/domain/grails/example/Books.groovy
package grails.example
class Books {
String name
static constraints = {
}
}
vi is a linux editing tool so use notepad and go to that path and as you can see I added
String name
save file now run:
$ grails generate-controller grails.example.Books
$ grails generate-view Books
$ grails generate-view grails.example.Books
$ grails run-app
As for general beginners guide, I came across this site today and have probably used in the past without noticing it...
http://grails.asia/grails-tutorial-for-beginners/
Best of luck and welcome to Grails. I have enjoyed every moment of working with it...
I'm trying to get my feet wet with grails, so I'm following a tutorial to get going with a sample project. I downloaded grails 2.3.2, added the environment variables for the command prompt commands, and successfully created a project by using >grails create-app teamwork. Calling >grails run-app after changing to the project directory successfully downloads all requisites, but then I get . I know that this exception doesn't reveal much, but do you guys have any ideas on why? I am running command prompt in admin mode, if that means anything.
Try not forking the JVM in BuildConfig.groovy, if I recall correctly. If that works, then check the JIRA issues for Grails 2.3.2.
I got the following error when trying to "generate-all" in STS :
| Loading Grails 2.2.1
| Configuring classpath.
| Environment set to development.....
| Packaging Grails application....
| Compiling 1 source files.....
| Packaging Grails application.
| Compiling 1 source files.....
| Domain class not found in grails-app/domain, trying hibernate mapped classes...
| Packaging Grails application.
| Compiling 1 source files.....
| Packaging Grails application.
| Compiling 1 source files.....
| No domain class found for name project.Smthg. Please try again and enter a valid domain class name
Now, my project is called "Project". I'm using Grails 2.2.1 and JDK1.7.
The first time I done a "generate-all" it worked, but since I deleted my generated .groovy files, I can't do it anymore, even with "clean" and "refresh dependencies".
I tried to reinstall STS, Groovy, I created a new workspace and deleted the .groovy and .grails directory in my HOME.
I don't know what to do, even with this fresh configuration I can't get it working...
In advance, thanks for helping me !
Reading your comment...
All right, I think I just solved the problem. Does anyone can confirm
that for this command (generate-all) we need to have the concerned
groovy file (here Smthg.groovy) already in the "domain" folder ? Thank
you.
...it looks like you are confused about what the generate-all command does. The manual tells us that the generate-all command:
Generates a controller, views, and a controller unit test for the
given domain class
The phrase of note here is for the given domain class. The domain class(es) you are trying to run generate-all on must exist before generate-all can work.