← Back to Upcase

Keeping params if a form dosn't save

(Scott Hollinshead) #1

I have created a meeting room booking form, which contains a calendar with the dates on.

When you click new booking the url has the date of the day the user clicks on in the parameters, so the url looks like http://www.example.com/bookings/new?date=2013-11-27.

This then sets the date column in my db when the form is submitted.

However when the form dosn’t save becasue it dosn’t match validations the date parameter gets removed from the url, how do I keep it in there so that the date can be saved?

My create action looks like this

def create
  @booking = Booking.new(booking_params)
  if @booking.save
    redirect_to root_url
    render "new"


(Brett McHargue) #2

It hasn’t been persisted, but the date should still be an attribute of @booking - initialise using that?

(Jacob Evan Shreve) #3

Is it important that it’s a get parameter?

brett is right, and it should be easiest to just use the attribute off @booking.

If you need it as a get parameter, though, I don’t think you can do that without losing the error messages provided by rendering :new instead of redirecting to it. Your best option then would be to make sure you always post to create with the get param URL.