Contents

[divider]

Adding Edit and Delete to our Records

Currently we don’t have an easy way to edit or delete data.  We can set up a quick solution to let Easygrid link to our record and use the built in views from the TradingCardInventory model to handle the show/edit/delete.

Let’s open up the /conf/EasygridConfig.groovy file.  We’re going to add something called a formatter.  See if one exists already, do a search for “formats” in this file.  If none show up, we’ll add our own.

To add a formats closure, go to the easygrid closure.  Within it (I put mine near the top) add this:

formats{

}

Open up that formats closure and add a named formatter, something like:

formats{
      editFormat = {
          “<a href=’/tradingCardInventory/show/${it}’>Edit</a>”
      }
}

Now we need to link to that formatter… open up your DashboardController and in the tradingCardInventoryGrid closure let’s add a column like this:

columns {
       id {
           label ‘Edit’
           type ‘id’
           formatName ‘editFormat’
       }

I added what’s in red above.  This should create a column with the title Edit, and it will have the word “edit” that links to the show page for your TradingCardInventoryController.  Make sense? We’re passing in the id, cuz we get the ID for free. type ‘id’ gives it to us.  We send that to the formatter who takes it as ${it} and puts it in the show url.

If we restart the app now, we should see a linked column value “edit” for each row. Click it.  You should go to the show page for that item.

Remember that with our access levels, if you update that show URL to a record we don’t own you should be redirected back to the home page.

Refactoring the Show and Edit Pages

Since we generated the show and edit pages for the TradingCardInventory model, we get a whole bunch of Grails stuff (links, art, colors, etc.)  We have links that link within Grails and a color scheme that may not be suitable.  You can edit those pages by edit files in:

/views/tradingcardinventory/

there’s a show.gsp and edit.gsp there that you can open up and modify.  Those files pull in data from _form.gsp (which is a template in that folder) as well.  The main.css file for Grails (which is found in the /web-app/css can also be modified to remove links/headers and change colors.

Make sure people who land on these pages can’t click around to get into other areas of the site we don’t want them to get to.

Mail

So you created users by the Bootstrap… registration will not work, try it. It will fail because there’s no email set up.  We need to get email working to get registration working.

The easiest way to get email is to add the Java Mail plugin.  This is actually called Mail.  Do a google search for Grails mail plugin.

As before, add the plugin compile version info into the /conf/BuildConfig.groovy file.

Compile your project and you’ll get the mail plugin pulled in.   We need to do some configuration to get it working:

The plugin has some documentation – review it, it shows how to add different mail settings with major providers.  The basic gist is that you edit the /conf/Config.groovy and add a closure like this:

grails {
    mail {
        host = “smtp.gmail.com”  //change to your mail host
        port = 465
        username = “[your login info]”  //add your username or email
        password = “[your login pass]” //add your password
        props = [“mail.smtp.auth”:”true”,
                “mail.smtp.socketFactory.port”:”465″,
                “mail.smtp.socketFactory.class”:”javax.net.ssl.SSLSocketFactory”,
                “mail.smtp.socketFactory.fallback”:”false”]
    }
}

Spring Security should just use it as is.  Just restart the app and try and register a new user.  You can start the registration process by going to a link that is secured (such as /dashboard), the basic login will have a link to register.  Fill out registration with your email and see if you get a reg email.

If you have a form that uses mail (like ‘contact us’) I’d recommend building a mail service.  This would benefit any controller that would need to send email at some point.  Maybe you decide you want to email your users based on some newsletter or other controller logic.  So put it in a central location, like a service.

However, I’ll go through simply making a mail controller for now, which could be updated into a service at some point later on.

Mail Controller

To create a controller, just Right-Click the Controller directory (in Intellij) and choose New Controller.  Give it the name EmailController.

In the service we could write a method like:

def sendMail(name, email, msg){
    String name = params.name
    String email = params.email
    String msg = params.msg
    sendMail {
          to “[YOUR EMAIL]”
          from “${email}”
          subject “Contact Us Response”
          body “${name} writes, ${msg}”
          redirect uri: ‘/’
 }

Now you just need to feed that data from a form.  Similar to what we did before.

On whatever page you have, you do a <g:form controller=”email” method=”post” action=”sendMail”>

Notice the controller isn’t fully spelled out, just the beginning of it’s name.  If you use Intellij it will auto complete for you.

In the form, just add a

<g:textField name=”name” required=”” size=”20″/>
<g:textField name=”email” required=”” size=”20″/>
<g:textArea name=”msg” required=””></g:textArea>
<g:submitButton  name=”send”  value=”Send” />

When you run the app, and fill out your form, it should email you, just like the registration system does.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Archives
Categories