Magat Law
Magat Law

Reputation: 5

Action in grails form is not working

I am trying to make a simple creation of account and when create button is clicked it should perform the action "create" but I get an error saying that
HTTP Status 404 - "/WEB-INF/grails-app/views/users/create.gsp" not found.

Here is my code block for the index.gsp

<!-- CREATEFORM -->
<div id="id02" class="modal">
  <g:form class="modal-content animate" controller="users" action="create">
    <div class="imgcontainer">
      <span onclick="document.getElementById('id02').style.display='none'" class="close" title="Close Modal">&times;</span>
        <div class="container" style="text-align:center">
            <b><h style="font-family: Arial, Helvetica, sans-serif; font-size: 30px">CREATE AN ACCOUNT</h></b><br/>
      <input type="text" placeholder="Enter Username" name="uname" required/>

      <input type="password" placeholder="Enter Password" name="psw" required/>

      <input type="text" placeholder="First Name" name="firstName" required/>

      <input type="text" placeholder="Last Name" name="lastName" required/>

      <input type="text" placeholder="Age" name="age" required/>
      <br/>
      <input type="date" placeholder="Birth Date" name="birthdate" required/>
      <br/>
      <input type="text" placeholder="Student Number" name="studno" required/><br/>

      <label>
        <input type="checkbox" checked="checked" name="remember"> Remember me</input>
      </label>
      <br/>
      <button type="submit" style="width: 100px; margin-right:10px;" >Create</input>
      <button type="button" onclick="document.getElementById('id02').style.display='none'" class="cancelbtn">Cancel</button>

    </div>
    </div>
  </g:form>
</div>

and Here is my code block for the usersController.

class usersController {    
    def index(){}
    def create()
    {
        new Users(userid:params.uname,password:params.psw).save()
        new UserInfo(studentno:params.studno,age:params.age,birth_date:params.birthdate,first_name:params.firstName,last_name:params.lastName,user_id:params.uname).save()
    }
}

And here's the error

Error Page

Upvotes: 0

Views: 661

Answers (3)

Nitin Dhomse
Nitin Dhomse

Reputation: 2622

In Grails for action (non gsp) you need to render something to the client, otherwise it executes everything inside that action, but returns 404 gsp not found response, if there is no render / redirection statement it consider that action as a gsp but it is not gsp actually, therefore it responds 404.

You could render something data like list / JSON or simple string as follows.

def create() {
 //your business logic is here
 render "operation performed successfully"

}

Upvotes: 0

Rahul Mahadik
Rahul Mahadik

Reputation: 12291

HTTP Status 404 - "/WEB-INF/grails-app/views/users/create.gsp" not found.

Because you are saving data using create action but after saving data there is no redirect option or create.gsp available. So grails will look first is there any redirect option available and try to redirect create.gsp

Check bellow code changes which will save user id to userInfo table and redirect to index page

So change your usersController

class usersController {
    def index() {}

    def create() {
        def user = new Users(userid: params.uname, password: params.psw).save()
        def userInfo = new UserInfo(studentno: params.studno, age: params.age, birth_date: params.birthdate, first_name: params.firstName, last_name: params.lastName, user_id: user.id).save()
        if (user && userInfo) {
            flash.message = "User created successfully"
            redirect action: 'index'
        } else {
            flash.message = "Problem in user creation"
            redirect action: 'index'
        }

    }
}

And add bellow code to your index.gsp

${flash.message}

There are many ways to redirect to pages. You can use render, redirect, chain, forward ..etc

Please check grails documentation for more information

Upvotes: 2

Anton Hlinisty
Anton Hlinisty

Reputation: 1467

First of all what grails version do you use? If the latest one (3.3.4) so this link might be helpful.

Speaking shortly: if you don't specify what to render explicitly - grails tries to find the view to display the result of action execution as:

grails-app/views/<controllerName>/<actionName>.gsp

Looks like you don't have one.

UPD

There are several ways to render action's output in grails controller. Define explicit content as:

def myAction() {
    ...
    render "Hello World!"
}

That will result in "white screen" with "Hello world!" text in top-left corner.

Another option is to create a gsp view in the conventional location as grails-app/views//.gsp - it will automatically be rendered after action execution if no specifying the render method invocation (like in the sample you've given)

The third option is to specify the model map:

def myAction() {
    ...
    [param1key: param1value, param2key: param2value, ...]
}

The model will be taken into account when rendering your gsp view.

Please read the documentation I gave link to. It's incredibly helpful.

Upvotes: 0

Related Questions