Topic: Newb search question

I am sure this is simple...any help would be appreciated!

I have a database with three columns...

Table: movies

|id|    |file_name|   |movie_date|

1      movie01.mov   2006-12-04
2      movie02.mov   2006-12-05
3      movie03.mov   2006-12-06
4      movie04.mov   2006-12-07

I used scaffolding to create a basic CRUD application, and am
looking to create a simple form that would have a drop down
date search form that could select a date, and have the filename
returned when the form is submitted. Example... selecting
"2006" "December" "5" in the form would result in "movie02.mov"
being displayed on the return screen.

So far my form looks like...

---------- start search.rhtml -------------

<%= start_form_tag :action => 'search', :id => @movie%>
<%= date_select 'movie', 'movie_date'%>
<%= submit_tag 'Search' %>
<%= end_form_tag %>

------------ end search.rhtml -------------

This looks pretty tidy, but of course nothing happens
when the form is submitted. Wondering how to create my
method within the movie_controller.rb file... so far I have...

def search
   @movie = Movie.find(params[:movie_date])
end

... but that has not gotten me too far... any suggestions?

Thanks!

J

Re: Newb search question

try

@movie = Movie.find_by_movie_date(params[:movie][:movie_date])

That's untested code, but that's the general gist of what you need to do.

Re: Newb search question

Thanks Tortoise... getting closer.

Still not able to have the result displayed...

I renamed my test form to searchform.rhtml ... it looks like this...

---------- start searchform.rhtml -------------

<%= start_form_tag :action => 'search', :id => @movie %>

<%= date_select 'movie', 'movie_date'%>


<%= submit_tag 'Search' %>
<%= end_form_tag %>

----------- end search.rhtml -------------

my movie_controller.rb now contains the method...

--------

  def search
    @movie = Movie.find_by_movie_date(params[:movie][:movie_date])
   end

--------

... and my search.rhtml is simply...

--------

<p>
<%= @movie %>
</p>

------

The form submits, and the searchform.rhtml is displayed, but the page is blank
with no results shown.

Any pointers? Thanks again.

- J

Re: Newb search question

"@movie" by itself wont produce any output. try something like

<p>
<%= @movie.file_name %>
</p>

Re: Newb search question

Thanks again Tortoise...

The form now submits with the error message returned...

--------

NoMethodError in Movie#search

Showing app/views/movie/search.rhtml where line #2 raised:

You have a nil object when you didn't expect it!
The error occured while evaluating nil.file_name

Extracted source (around line #2):

1: <p>
2:  <%= @movie.file_name %>
3: </p>
4:

---------

I have a number of records in the table that all have a filename
associated with the date I am selecting from the pull down menu...
any other ideas? Thanks man.

- J

Re: Newb search question

I played around with my own form that uses date_select.

It's returning three entries in the parameter hash, one for the year, month and day.

To access each piece, you need to do params[:movie]["movie_date(1i)"], params[:movie]["movie_date(2i)"] and params[:movie][:movie_date(3i)"]

As you can see in my parameters dump for my form:

{"city"=>"Morgantown", "name"=>"delila", "birth_date(1i)"=>"1975", "birth_date(2i)"=>"8", "about_me"=>"", "birth_date(3i)"=>"12", "country"=>"", "new_avatar_image"=>#, "state"=>"WV"}

so this should work, but dear lord we are barking up the wrong tree here. There simply has to be a better way to do this...

hash = params[:movie]
date = "#{hash['movie_date(1i)']}-#{hash['movie_date(2i)']}-#{hash['movie_date(3i)']}"
@movie = Movie.find_by_movie_date(date)

I don't at all recommend you use that, there's got to be a better way to accomplish this.

Re: Newb search question

I've never cared for the date/time selectors. There's actually two different date selectors: date_select and select_date (nice naming eh?). The first one, date_select, is used for editing a model's date attribute, the second one, select_date is generic and used for anything else.

Since you aren't actually editing a model's attributes here, I suggest using select_date. The form field naming is a little cleaner too.

Sadly there's no fancy way to parse this (that I know of). Here's one way:

Time.local(params[:date][:year], params[:date][:month], params[:date][:day])

If you want you can extend the Hash class to do this:

class Hash
  def to_time
    Time.local(self[:year], self[:month], self[:day]) # probably want to add hours, minutes and seconds
  end
end

Then you can use it like this:

@movie = Movie.find_by_movie_date(params[:date].to_time)

Railscasts - Free Ruby on Rails Screencasts