0
votes

I am building a reservation / booking system with Grails.

I have a Reservation Class and a Customer Class. There is a one (customer) to many (reservations) relationship. I wanted to make sure that new reservations would only be made from within the customer show view so I have edited the views to make this the only place from where you can create a new reservation.

This works and the Create Reservation form also works with drop down selectors for the Customers and Products. I can make a reservation this way but I have to choose the customer - I want the customer field to be automatically populated with the customer name from where I click the new reservation button.

Currently in my _form.gsp (used by Reservation create.gsp) I have the following line that gives me the aforementioned select function

<g:select id="customer" name="customer.id" from="${net.kanootours.Customer.list()}" optionKey="id" required="" value="${reservationInstance?.customer?.id}" class="many-to-one"/>

I thought the part: value="${reservationInstance?.customer?.id}" would set the field to the value of the current customer but it does not (the field is just populated with the first customer name).

How can I get the name of the customer record that I am calling the New Reservation from to populate in the customer field? What would be the correct tag to be using in this situation if not g:select?

For the end result I just want the Customer name in the new reservation and do not need the ability to change that name to another.

I have tried to use g:field and g:fieldValue but was not able to get these to work either.

I also thought of the possibility of sending the customer id as a param from the calling g:link - but I'm not sure how to process that.


UPDATE / SOLUTION:

Greggs comments below helped me find the solution for this and also the book GRAILS 2: A Quick Start Guide , Chpater "Forum Messages and UI Tricks".

To get this to work I added the following :

In Reservation _form.gsp :

<g:hiddenField name="customer.id" value="${customerInstance?.customer?.id}" />
<g:hiddenField name="customer.firstName" value="${customerInstance?.customer?.firstName}" />
<g:hiddenField name="customer.lastName" value="${customerInstance?.customer?.lastName}" />


N.B - I did not need all three - just the ID , but I used this for a better display title on the Reservation Create page.

I changed the link in my Customer Show page (New Reservation Link) to be :

<li><g:link controller="reservation" class="create" action="create" 
params='["customer.id":"${customerInstance?.id}","customer.firstName":"${customerInstance?.firstName}","customer.lastName":"${customerInstance?.lastName}"]'>New Reservation</g:link></li>

This was the part I had trouble with the most because the customer.id part was throwing lots of errors in every format I tried to put it in the params. This is is where the book helped me and it pointed out that it needed to be enclosed by "" as it had a '.' in the param. Once I discovered this I could work my way through any other problems

I also added to reservation create view :

<h1><g:message code="default.create.label" args="[entityName]"  /> for ${reservationInstance?.customer?.firstName} ${reservationInstance?.customer?.lastName} ID: ${reservationInstance?.customer?.id}</h1>

Which I used as a test to make sure the params where being passed correctly. I could then work on ensuring that the customer.id field was sent to the create action in the controller correctly.

To achieve that I used

<g:field type="text" name="customer.id"value="${reservationInstance?.customer?.id}" readonly="readonly"/>
1
thanks Burt - im still learning how present questions here., Your editing is helpful and I will use this to refer to in future - boliviab

1 Answers

0
votes

I'm taking a bit of a guess here as I'm not fully following your question all the way through. So my assumption is that you're on a Customer screen and you want to click a New Reservation button and have the customer already associated to said Reservation. If that is true, then the following should get you headed in the right direction.

On the Customer form:

<g:link controller="reservation" action="create" params="[customer.id: customer.id]">Create Reservation</g:link>

RegistrationController

def create() {
   // params.customer.id will auto populate in this reservation instance
   def reservation = new Reservation(params)
   ...
} 

Registration form:

<g:hiddenField name="customer.id" value="${reservation.customer.id" />
Customer Name: ${reservation.customer.name}

In the above code, Customer will be available in the Reservation object. Then, you store the customer.id as a hidden field so that when you submit this reservation to your RegistrationController.save() action, it will again auto-populate the Reservation with the correct Customer id. If you need to store something different about a Customer in Reservation, the same technique applies. You just have to keep passing it through all the requests.