I just started playing with rails this week and am working on porting our existing website to rails in our navigation we have a list that has the days of the week that link to the articles for those days. this seems to be working when I had it in the template it would give me the array I was looking for so I decided to move it to the helper so that I could use it on all my templates. Application_helper.rb ------------------------------------------------ def days t=Time.now() #days [''Monday'',''Tuesday'',''Wednesday'',''Thursday'',''Friday'',''Saturday'',''Sunday''] #YY,MM,DD,h,m,s days = Array.new for i in 1..8 d = Time.local(t.year,t.month,t.day-i) #n = d.to_i today = d.strftime("%A") days.insert(''today'',d.to_i) end end ----------------------------------------------------------- I know this is not correct but it is what I am looking to do. when the above code was in the template minus the def these lines would spit out the error about converting strings to integers. application.rhtml <li><a href="/index/<%= days[''Monday''] %>">Monday</a></li> <li><a href="/index/<%= days[''Tuesday''] %>">Tuesday</a></li> <li><a href="/index/<%= days[''Wednesday''] %>">Wednesday</a></li> <li><a href="/index/<%= days[''Thursday''] %>">Thursday</a></li> <li><a href="/index/<%= days[''Friday''] %>">Friday</a></li> <li><a href="/index/<%= days[''Saturday''] %>">Saturday</a></li> <li><a href="/index/<%= days[''Sunday''] %>">Sunday</a></li> I am coming from about 7 years of php background so go easy on me :) Thanks for your help _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Spectre013 wrote:> > in our navigation we have a list that has the days of the week that link > to the articles for those days.I don''t know about your specific case, but since nobody replied, I just thought I''d chime in that in Ruby land we use ''class Hash'' for Associative Arrays. HTH.. --Steve
Helpers are just methods so you can''t really access them this way. You can call days() inside your template and assign it to a variable: <% days = get_days() %> On 12/16/05, Spectre013 <spectre013-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I just started playing with rails this week and am working on porting our > existing website to rails > > in our navigation we have a list that has the days of the week that link to > the articles for those days. > > this seems to be working when I had it in the template it would give me the > array I was looking for so I decided to move it to the helper so that I > could use it on all my templates. > Application_helper.rb > ------------------------------------------------ > def days > t=Time.now() > #days > [''Monday'',''Tuesday'',''Wednesday'',''Thursday'',''Friday'',''Saturday'',''Sunday''] > #YY,MM,DD,h,m,s > days = Array.new > for i in 1..8 > d = Time.local(t.year,t.month,t.day-i) > #n = d.to_i > today = d.strftime("%A") > days.insert(''today'',d.to_i ) > end > end > ----------------------------------------------------------- > > > > I know this is not correct but it is what I am looking to do. > when the above code was in the template minus the def these lines would spit > out the error about converting strings to integers. > > application.rhtml > > <li><a href="/index/<%= days[''Monday''] %>">Monday</a></li> > <li><a href="/index/<%= days[''Tuesday''] %>">Tuesday</a></li> > <li><a href="/index/<%= days[''Wednesday''] %>">Wednesday</a></li> > <li><a href="/index/<%= days[''Thursday''] %>">Thursday</a></li> > <li><a href="/index/<%= days[''Friday''] %>">Friday</a></li> > <li><a href="/index/<%= days[''Saturday''] %>">Saturday</a></li> > <li><a href="/index/<%= days[''Sunday''] %>">Sunday</a></li> > > I am coming from about 7 years of php background so go easy on me :) > > Thanks for your help > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
On 16-dec-2005, at 16:51, Spectre013 wrote:> I just started playing with rails this week and am working on > porting our existing website to rails > > in our navigation we have a list that has the days of the week that > link to the articles for those days. > > this seems to be working when I had it in the template it would > give me the array I was looking for so I decided to move it to the > helper so that I could use it on all my templates. > Application_helper.rb > ------------------------------------------------ > def days > t=Time.now() > #days = > [''Monday'',''Tuesday'',''Wednesday'',''Thursday'',''Friday'',''Saturday'',''Sunday > ''] > #YY,MM,DD,h,m,s > days = Array.new > for i in 1..8 > d = Time.local(t.year,t.month,t.day-i) > #n = d.to_i > today = d.strftime("%A") > days.insert(''today'',d.to_i ) > end > end > ----------------------------------------------------------- > > > > I know this is not correct but it is what I am looking to do. > when the above code was in the template minus the def these lines > would spit out the error about converting strings to integers. > > application.rhtml > > <li><a href="/index/<%= days[''Monday''] %>">Monday</a></li> > <li><a href="/index/<%= days[''Tuesday''] %>">Tuesday</a></li> > <li><a href="/index/<%= days[''Wednesday''] %>">Wednesday</a></li> > <li><a href="/index/<%= days[''Thursday''] %>">Thursday</a></li> > <li><a href="/index/<%= days[''Friday''] %>">Friday</a></li> > <li><a href="/index/<%= days[''Saturday''] %>">Saturday</a></li> > <li><a href="/index/<%= days[''Sunday''] %>">Sunday</a></li> > > I am coming from about 7 years of php background so go easy on me :) > > Thanks for your helpYou need Hash.new (or just {} ) instead of Array.new. Array will complain if you feed it Strings as keys - moreover, when you set a key in an Array which does not exist, the interval to the last existing key will be filled with nils. For example: ar = [1,2,3,4,5] ar[7] = 3 ar # will be [1, 2, 3, 4, 5, nil, nil, 3] Beware though that Ruby hashes are UN-ordered (which means that you can''t set months there and then iterate through them in the same order) myhash = {} hash[:key1] = 2 hash[:key2] = 4 etc. but when you iterate over them with each_pair you might as well get key2 first.
Spectre013 wrote:> I just started playing with rails this week and am working on porting > our > existing website to rails > > in our navigation we have a list that has the days of the week that link > to > the articles for those days. > > this seems to be working when I had it in the template it would give me > the > array I was looking for so I decided to move it to the helper so that I > could use it on all my templates. > > <code elided/>You must use a Hash instead of an Array in this case (beware, not tested): # This should work def weekdays() @weekdays = {} 7.times {|i| # I believe Rails provides Time#days, or just sub 60*60*24*i d = Time.now - i.days @weekdays[d.strftime("%A")] = d.to_i } @weekdays end # You could be fancy and use Enumerable#inject def weekdays2() # ''inject'' an empty Hash to be filled (0...7).to_a.inject({}) {|hash, i| d = Time.now - i.days hash[d.strftime("%A")] = d.to_i hash } end Then you can call it as weekdays[''Monday''] # Or @days = weekdays @days[''Tuesday'']> I know this is not correct but it is what I am looking to do. > when the above code was in the template minus the def these lines would > spit > out the error about converting strings to integers.The error is simply due to Arrays only accepting numeric indices.> > Thanks for your helpE -- Posted via http://www.ruby-forum.com/.
Eero Saynatkari wrote:> Spectre013 wrote: > > <snip earlier/>I was modestly intrigued by the question.. here is the more-or-less final version I came up with since I wanted a one-liner: require ''date'' require ''time'' def weekdays() ((Date.today - 6)..today).inject({}) {|h, d| h[d.strftime("%A")] = Time.parse(d).to_i; h} end # weekdays() weekdays[''Monday''] # etc. E -- Posted via http://www.ruby-forum.com/.
Eero Saynatkari wrote:> Spectre013 wrote: > > <snip earlier/>I was modestly intrigued by the question.. here is the more-or-less final version I came up with since I wanted a one-liner: require ''date'' require ''time'' def weekdays() ((Date.today - 6)..today).inject({}) {|h, d| h[d.strftime("%A")] = Time.parse(d).to_i; h} end # weekdays() weekdays[''Monday''] # etc. Of course, it might be desirable to use some other kind of URL rather than Time#to_i, for example just ''2005/12/18'', for example. That should be simple enough to substitute for the Time.parse call. E -- Posted via http://www.ruby-forum.com/.
If I have a collection_select in my form, should it automatically update the id for the table it is populating with the foreign key? I don''t think it is, because the code as it is now is leaving person_id as nil. Any suggestions? in _form.rthml <p><label for="person_id">Paid by</label><br/> <%= @people = Person.find(:all ) collection_select(:person, :fullname, @people, :id, :fullname) %> models: class Bill < ActiveRecord::Base belongs_to :person end class Person < ActiveRecord::Base has_many :bills end Thanks, - Ivan Storck
On 12/18/05, Ivan Storck <ivan-pg4k/U1+E1N1fM1P2I35rWoLBQzVVOGK@public.gmane.org> wrote:> If I have a collection_select in my form, should it automatically > update the id for the table it is populating with the foreign key? I > don''t think it is, because the code as it is now is leaving person_id > as nil. Any suggestions? >Assuming @bill is the instance variable for the Bill object, try: <p><label for="bill_person_id">Paid by</label><br/> <%= select ''bill'', ''person_id'', Person.find(:all).collect {|p| [p.fullname, p.id]} %>
Eero, This is the code that I used as I cold follow it a little better. I had just read about hashes after I read your messages. I am still learning alot about Ruby and Rails. # This should work def weekdays() @weekdays = {} 7.times {|i| # I believe Rails provides Time#days, or just sub 60*60*24*i d = Time.now - i.days @weekdays[d.strftime("%A")] = d.to_i } @weekdays end Thanks for your help Brian On 12/17/05, Eero Saynatkari <ruby-forum-reg-hRtevi7K+EU+Va1GwOuvDg@public.gmane.org> wrote:> > Eero Saynatkari wrote: > > Spectre013 wrote: > > > > <snip earlier/> > > I was modestly intrigued by the question.. > here is the more-or-less final version I > came up with since I wanted a one-liner: > > require ''date'' > require ''time'' > > def weekdays() > ((Date.today - 6)..today).inject({}) {|h, d| h[d.strftime("%A")] > Time.parse(d).to_i; h} > end # weekdays() > > weekdays[''Monday''] # etc. > > > > E > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Spectre013 wrote:> Eero, > > This is the code that I used as I cold follow it a little better. I had > just > read about hashes after I read your messages. I am still learning alot > about > Ruby and Rails. > > # This should work > def weekdays() > @weekdays = {} > 7.times {|i| > # I believe Rails provides Time#days, or just sub 60*60*24*i > d = Time.now - i.days > @weekdays[d.strftime("%A")] = d.to_i > } > @weekdays > end > > Thanks for your helpSo long as it works! :) Here is an annotated version, and you can get more information about the structures from http://www.ruby-doc.org or just using ri from your command-line (try ri --help): ((Date.today - 6)..Date.today) This is a Range object starting from today - 6 days, in other words a week ago. It consists of each day between that -6 days and today. If you wanted today to be excluded and the previous seven days instead, ((Date.today - 7)...Date.today) would work. Notice three dots instead of two. This means the last value is to be excluded. .inject({}) {|h,d| ... #inject is a method in the module Enumerable (also mixed in by Hashes and Arrays). It takes a collection such as the Date Range above. It iterates over that collection while accumulating some information. In our case, the initial value given for that accumulation is an empty Hash. Inside the block, the first value is the accumulation and the second value is the current element -in this case, h is the Hash and d is the Date object in sequence. h[d.strftime("%A")] = Time.parse(d).to_i; h} Here are a few things: as you see, we store items in the Hash, specifically the key is the name of the weekday and the value we come up with by creating a new Time object by parsing it from the Date object, which we then in turn extract the integral representation from as in your original example. Finally, the Hash is returned as the value of the block. For #inject, each iteration the accumulation value is set to be the value returned by the previous iteration. Because we want to keep accumulating data in the same Hash, we explicitly give it as the value of the block. Once the iteration is complete, the final accumulation value is returned as the result of #inject. In our case this is the Hash consisting of {<day name> => <integer>} mappings that you can use. E> Brian-- Posted via http://www.ruby-forum.com/.