Topic: Sorting by date and finding index from item date?

Hey guys, I just started ruby around 1 month ago for my final project for school.

I understand the variables and methods and rthml and stuff, but there's quite a few (actually many hehe) questions that I have.

I've been trying to do this for around 2 weeks now, and all its doing is aggrivating me because whenever I try to find help, I feel like I have to learn 2 new things to accomplish my first task. Anyways, enough rambling.

I have to make an itinerary with stored events.
I have the Main itinerary page, and I can select an itinerary.date link that will take me to a new page that lists all the stored items in the database.

1) How would I find out which date was selected by the user on the main page so I can display ONLY the items that have that certain date from the database?

2) I know these might seem like baby questions, but how do I sort my main page so the dates are descending? And how do I put it in a better form then 2007-01-27? (IE: Can I make it show the date as January 27, 2007 instead?)

Thanks all, really appreciated and I don't know why i'm having so much trouble with ruby...

    - SideFFect -

Re: Sorting by date and finding index from item date?

Are you fairly new to programming and scripting in general? It may be less frusterating to learn Ruby on its own first. Ruby on Rails has an extremely high learning curve if you are new to programming.

SideFFect wrote:

1) How would I find out which date was selected by the user on the main page so I can display ONLY the items that have that certain date from the database?

It depends how the date was selected. Can you post the main page where the user selects the date? You would use the params[] hash, but I can't provide any details without knowing how the date is selected.

SideFFect wrote:

2) I know these might seem like baby questions, but how do I sort my main page so the dates are descending? And how do I put it in a better form then 2007-01-27? (IE: Can I make it show the date as January 27, 2007 instead?)

You would sort it like this:

@itineraries = Itinerary.find(:all, :order => 'date desc') # if "date" is the name of the column

You can use the "strftime" method to present the date in many different ways. For example:

<%= itinerary.date.strftime('%B %d, %Y') %>

Railscasts - Free Ruby on Rails Screencasts

Re: Sorting by date and finding index from item date?

Actually, no, I'm in my final year in software engineering, but I haven't done a course in programming in a while (its all software metrics and documentation classes).

I know java, C++, C, etc... But ruby just is getting the better of me because I don't know what functions I can use and/or how they are used.

Thanks for the code for the date, but as the main page goes, what do you want me to list? the code for my itinerary controller? or the list.rhtml (list is being used as the main page)

Also, there is no date selector in place. All I have is a list of items on the main page like this:

2007-01-27
2007-01-21
2007-01-21

Each of those dates are selectable and each take me to /itinerary/dates/1, /itinerary/dates/2, etc.



    - SideFFect -

Last edited by SideFFect (2007-01-27 23:25:42)

Re: Sorting by date and finding index from item date?

If you post the code in list.rhtml that will help.

Does the number in the URL represent the id of something? Do you have a date model? You can get the number using params[:id].

Railscasts - Free Ruby on Rails Screencasts

Re: Sorting by date and finding index from item date?

ryanb wrote:

If you post the code in list.rhtml that will help.

Does the number in the URL represent the id of something? Do you have a date model? You can get the number using params[:id].

<body>

<h1>CityPoint Mobile - Itinerary</h1>
<table border="1">
<tr>
  <td width="80%"><p align="center"><i><b>Please select your itinerary date</b></i></td>
</tr>

<% @itineraries.each do |itinerary| %>
  <tr>
   <td><p align="center"><%= link_to itinerary.date.strftime('%B %d, %Y'), :action => "dates", :id => itinerary.id %></td>
  </tr>
<% end %>
</table>
</body>


That's list.rhtml

And the number in the url is simply the id from the database whenever I make a new itinerary item w/ itinerary/new  page.

Also, how do I view the scaffolding pages? Meaning, how do I see how scaffolding edit or new looks like? (code wise, and where it's stored in ruby)

Thanks for your help!

         - SideFFect -

Re: Sorting by date and finding index from item date?

I recommend you link to the "show" action from the itinerary date:

<td><p align="center"><%= link_to itinerary.date.strftime('%B %d, %Y'), :action => "show", :id => itinerary.id %></td>

The "show" action made by scaffolding should already have the details of the itinerary as well as an edit link. Of course you can customize this by editing show.rhtml, and I recommend you do.

Railscasts - Free Ruby on Rails Screencasts

Re: Sorting by date and finding index from item date?

I already had it linked to 'show' but all it did was display only one of the records in the database.

Isn't there anyway to have the show page show all the records for the date that were chosen from the main page?

Like if someone picks 2007-01-28, it shows all the records that were created on that day, not just one.


   - SideFFect -

Last edited by SideFFect (2007-01-28 14:25:23)

Re: Sorting by date and finding index from item date?

I see what you mean now. Yes, it is possible by passing the date in the URL in the list.rhtml file:

 <% @itineraries.each do |itinerary| %>
  <tr>
   <td><p align="center"><%= link_to itinerary.date.strftime('%B %d, %Y'), :action => "dates", :date =>itinerary.date.strftime('%Y-%m-%d') %></td>
  </tr>
<% end %>

And then you can search based on the date in the "dates" action:

# in controller
def dates
  @itineraries = Itinerary.find(:all, :conditions => ['date=?', params[:date]])
end

You can then loop through this @itineraries array in the dates.rhtml partial to display the itineraries.

Railscasts - Free Ruby on Rails Screencasts

Re: Sorting by date and finding index from item date?

Dude! Thanks! It worked like a charm!

Now, I understand why it worked, but can I have a reference as to what words I can and can't use in ruby? And how they would be used?

I think that's my only problem...I'm afraid to try anything cause I don't know what words are reserved or what can and can't be used.

Thanks for your help man, now I just have to install a calendar plugin and I'm pretty much set (for the basics at least)


     - SideFFect -

Re: Sorting by date and finding index from item date?

See this list. Most of these are class/model names that are known to cause problems. You don't have to worry too much about reserved words for variable names and passing parameters in the URL.

Railscasts - Free Ruby on Rails Screencasts

Re: Sorting by date and finding index from item date?

Hmm thanks, I'll check it out!

So besides these keywords, anything I define as a :name I can use as a condition or evaluator in the controller? I know :action does something obviously, and you used :date to be passed as a parameter. So that means I could have used any word there, or date was used because my column in sql was date also?

Re: Sorting by date and finding index from item date?

You can use any word to pass the date to the controller. It's just the params[:date] needs to be changed to that word so it fetches the proper date.

":controller", and ":action" are the two you shouldn't use when passing custom data through a URL, other then that you can use pretty much anything. Of course use :controller and :action if you are literally specifying the controller and action to be used. I probably just confused you now, sorry.

Railscasts - Free Ruby on Rails Screencasts

Re: Sorting by date and finding index from item date?

Heh no you didn't consfuse me don't worry tongue

Sorry I took so long to respond, got really caught up with school.

Thanks for all the help though, this is a great forum for rails help!


     - SideFFect -