Note that there are some explanatory texts on larger screens.

plurals
  1. POGrails UrlMappings for a telephone number
    text
    copied!<p>I'd like to set it so when a user visits the application with a telephone number in the URL Grails directs the user to the specified profile, for example: </p> <p><code>localhost:8080/Application/profile/07871216969 -&gt; /user/show/User.findByUserTelephone(07871216969)</code></p> <p>or preferably </p> <p><code>localhost:8080/Application/07871216969 -&gt; /user/show/User.findByUserTelephone(07871216969)</code></p> <p>However, I have no idea how to reference the telephone number in the URL when calling the findByUserTelephone closure; I've noticed something like: </p> <p><code>/profile/$telephoneNumber</code> however I'm not entirely sure how that works out. </p> <p>Also, as the /user/show action requires an id in the parameters I am not sure if the findUserByTelephone closure is any use as it returns the User as an object, and I can't seem to either getId() or .id the object to retrieve the id. </p> <p><strong>SOLVED/SOLUTION:</strong> </p> <p>I solved this by creating another action in the Controller called <code>profile</code>, this action then had code similar to the following: </p> <pre><code>def profile = { if(User.findByUserTelephone(params.userTelephone)) { def userInstance = User.findByUserTelephone(params.userTelephone) if (!userInstance) { flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'User'), params.id])}" redirect(action: "list") } else { [userInstance: userInstance] } } else { render("Sorry, that user wasn't found in our database.") } } </code></pre> <p>I then created the following UrlMapping entry: </p> <pre><code>`"/$userTelephone"(controller: "user", action: "profile")` </code></pre> <p>Thus, when a user enters a telephone number into the system (or any string after the <code>/</code>) it will route the user to the user/profile action, which will attempt to find a user by their telephone number. If successful, will show the users details otherwise will show an error message.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload