Reputation: 2833
i have a specific question;
I'm newbie on grails i'm trying to write some test app with this scenario;
I have 2 Domain Classes
1- Car 2- Registration
I want to add some cars with brand, model etc. And user will choose from list some car and click to rent car button. When user will click to rent car button will open the registration/create page here i want to get car:id of user selected car and user will enter some extra information from himself like Your Name and when user will click to save button on registration/list i need to see registrations with car:id and your name entry
In car class i have list of cars, with this field here is my car model;
package rentme
class Car {
String brand
String model
String fuelType
BigDecimal pricePerDay
String busy
static constraints = {
brand(inList:["AUDI", "BMW", "MERCEDES", "NISSAN", "HONDA", "FORD"])
model()
fuelType(inList:["FUEL", "DIESEL", "AUTOGAS"])
pricePerDay(min:0.0, max:1000.0)
busy(inList:["YES", "NO"])
}
}
And i have Registration class,
package rentme
class Registration {
String yourName
static constraints = {
yourName()
}
}
So what i did ,
i add the cars/index.gsp this link in g:each;
<g:each in="${carList}" var="car">
<p>${car.id} </p>
<g:link action="create" controller="registration" params="${[car: car, carId : car.id ]}">
Rent Car
</g:link>
</g:each>
and modified Registration Controller like that;
def index(Integer max) {
params.max = Math.min(max ?: 10, 100)
respond Car.list(params), model:[carCount: Car.count()]
}
Now what happened; When i click to rent button page redirect to registration/create page with the catching id which car user selected;
here is my registration/create.gsp
<g:form action="save">
<div><p>Car ID : ${car.id} </p></div>
<fieldset class="form">
<f:all bean="registration"/>
</fieldset>
<fieldset class="buttons">
<g:submitButton name="create" class="save" value="${message(code: 'default.button.create.label', default: 'Create')}" />
</fieldset>
</g:form>
And when fill your name field and click to save button everything is okey, So like it's working but not :)
because when i check the my db i don't see there selected car id.
mysql> SELECT * FROM registration;
+----+---------+--------------+
| id | version | your_name |
+----+---------+--------------+
| 12 | 0 | Cihan Zengin |
| 13 | 0 | Cihan |
| 14 | 0 | ndlkdkldnkl |
+----+---------+--------------+
But when i click to registration/index or registration/show there is have just your name value but i want to see there car:id too.
How to do that? Please someone tell me that because i can't understand and can't found nowhere something like that in internet
Upvotes: 0
Views: 1085
Reputation: 161
Let's assume for every registration, there will only be one car and one person each.
Firstly, for the Registration class:
package rentme
class Registration {
String yourName
Car rentedCar
static constraints = {
yourName()
}
}
As you can see, I have added Car inside, so for every registration object, there will be a Car object. In your database for Registration table, a new column 'CAR_ID" will be added automatically.
Next, for the cars/index.gsp each:
<g:each in="${carList}" var="car">
<p>${car.id} </p>
<g:link action="create" controller="registration" params="${[carId : car.id ]}">
Rent Car
</g:link>
</g:each>
The action="create"
and controller="registration"
means method create()
in registrationController
will be called, and if found or not found, registration/create.gsp will be rendered. In registrationController, i'm not sure what's your index method for, but just add these:
def create() {
def selectedCar = Car.get(params.carId)
[car: selectedCar]
}
def save() {
def selectedCar = Car.get(params.carId)
def registration = new Registration(
yourName: params.yourName,
car: selectedCar
).save()
}
Then, in registration/create.gsp:
<g:form action="save">
<div><p>Car ID : ${car.id} </p></div>
<fieldset class="form">
<f:all bean="registration"/>
</fieldset>
<fieldset class="buttons">
<input type="hidden" name="carId" id="carId" value="${car?.id}"/>
<input type="text" name="yourName" required>
<g:submitButton name="create" class="save" value="${message(code: 'default.button.create.label', default: 'Create')}" />
</fieldset>
</g:form>
The create() method gets the params of carId from car/index.gsp and use it to get the Car object, and rendered in registration/create.gsp
The hidden field in registration/create.gsp ensure that the car id will be passed to the controller when the form is submitted. Therefore, when the submitButton is clicked, save() method in Registration Controller will be called and a Registration object with the retrieved Car Object will be saved.
There's a lot of bad practices in what we did here but it should work. Let me know if this is still not clear :)
Upvotes: 2