Topic: Car rental application question

Hi
im making a simple car rental application using ruby on rails

i have three tables with one to one relationships (a user can only make one reservation and only one car can be reserved at a time)

Cars - holds the car details
Reservations - holds the reservation details
Users - holds the user details

on my car details page, i want to to be able to change the car's availablity option by a checkbox, so if the checkbox is checked, the car is up for reservation but if it is unchecked, the car cannot be reserved..

is this done through a method?

sorry very new at this

thank you

Re: Car rental application question

Seeing as you are keeping record of reservations through your reservations model/table. You can perform a simple check wether there is a current reservation for the car. However, if you want to do this with a checkbox, you need to add a is_available (example) boolean to your table and use the checkbox form object to manage this.

Re: Car rental application question

right, thanks. could you help me with the coding of the checkbox object? i have no idea sad

my tables consist of these columns:

Cars: id, make, model, colour, is_available
Reservations: id, car_id, user_id, end_date
Users: id, login, email, pw

thanks again

Last edited by nacholibre (2010-01-07 05:50:56)

Re: Car rental application question

Hi,

The code for the checkbox in a form (taken from: http://api.rubyonrails.org/classes/Acti … #M001612):

<% form_for @car do |f| %>
   <%= f.check_box :is_available %>
<% end %>

Something along the lines of that smile Your standard CRUD actions (if you have them already) should take care of saving it to the DB, if you have any more questions regarding how to set up your models and controller actions properly.. feel free to ask smile.

Re: Car rental application question

hi and thanks again but i'm talking about the code behind the checkbox in the controller class that makes the application understand to make cars available to reserve if the checkbox is checked and unavailable if not.

so it would look something like this

def checkbox_availability

if checkbox value = "1"

(code that i dont know)

else

(code that i dont know)

end

sorry i'm very new to ruby and programming overall sad

Re: Car rental application question

I don't really quite understand what you mean, what do you mean with "makes the application understand to make cars available to reserve if the checkbox is checked and unavailable if not".. isn't that the point of having the check_box?

Re: Car rental application question

ok i think i thinking way out of the box

i want the user (not admin) to only view the cars that are available. The cars that are unavailable i want hidden.
the admin can view both and edit the availability (so check and uncheck the box) which he can atm.

so i think i need a code somewhere, something like,

if checkbox value is true
show
else
hide
end

if this is the right way of doing it, where does this code go? and how is it written properly.

thanks for your help again!!!!

Re: Car rental application question

implement this through the find method you are performing to find all cars for normal users,

Car.find(:all, :conditions => ["is_available = 1"])

Re: Car rental application question

you're an absolute legend, it wasn't exactly correct but close enough and i managed to figure it out
the correct code =   @avail = Car.find(:all, :conditions => ['is_available = ?', true])

thank you so much!

now gotta get on with the rest of it.. is it okay to ask here again if i hit any problems? (which i will) lol

Re: Car rental application question

ok i've already hit a brick wall sad
i'm trying to get the actual reservations working now so i decided to make a new column in my car details table named 'is_reserved' (bool). now what i'm trying to do is to add a "reserve" button next to my car details and when it is clicked, i want it to change the value in the 'is_reserved' column from false to true.
i got as far as coming up with this code: @car = Car.find(params[:id]).update_attribute('is_reserved', true) which works and does change the attribute from false to true, but i dont get where i need to put it. currently. its in the update method, but now if i want to edit my car details, the 'is_reserved' attribute automatically changes to true because that code is there...

do i need to make a new method called reserve? if so, how do i link it to my form?

Last edited by nacholibre (2010-01-08 08:15:34)