views:

16

answers:

1

Hi

I have the following method that retrieves all the appointments between 2 dates:

    def self.search_app(start_date, end_date)
    if start_date
       start_date = Date.new(start_date['(1i)'].to_i, start_date['(2i)'].to_i, start_date['(3i)'].to_i)
       end_date = Date.new(end_date['(1i)'].to_i, end_date['(2i)'].to_i, end_date['(3i)'].to_i)
       find(:all, :conditions => {:date_of_appointment => (start_date)..(end_date) })
    else
      find(:all)
    end
  end

and in the view:

<div id="search">
<% form_tag doctor_appointments_path(@person), :method => "get" do %>
    <span> From </span>
    <%= date_select :start_date, :start_date %>
    <span> Until </span>
    <%= date_select :end_date, :end_date %>
    <%= submit_tag "Search", :name => nil %>
<% end %>
</div>

controller code:

def index
  @appointments = person.appointments.search_app(params[:start_date], params[:end_date])
end

method works great the first time its called, but if i try searching again i get an argument error:

Invalid date

{"start_date"=>{"start_date(1i)"=>"2010",
 "start_date(2i)"=>"8",
 "start_date(3i)"=>"5"},
 "doctor_id"=>"1",
 "end_date"=>{"end_date(3i)"=>"17",
 "end_date(1i)"=>"2010",
 "end_date(2i)"=>"8"}}

somehow its passing the doctor Id with the Date.new method?

any ideas how i can fix this?

thanks

+1  A: 
date_select :start_date, :start_date

is telling rails that there's a @start_date object that will be available and that the .start_date attribute will be provided by the date select.

You would probably be better off using select_date to which you can pass a date to use and you'll need a prefix to differentiate between your start and end dates. For example:

select_date Date.current, :prefix => 'start'
Shadwell
thank you very much
Mo