Hi, Most likely it is just my poor knowlege of Ruby, but the first helper that I have created does not work how I expected it. <%= image_size_tags(@image.width, @image.height) %> def image_size_tags (width, height) if width > 320 factor = width / 320 width = width / factor height = height / factor end if height > 250 factor = height / 250 width = width / factor height = height / factor end return "width=#{width} height=#{height}" end it returns the original width and height even if they are bigger then 320 & 250. Kind Regards, Herbert
You might want to try this: def image_size_tags (width, height) if width > 320 factor = width / 320.0 width = width / factor height = height / factor end if height > 250 factor = height / 250.0 width = width / factor height = height / factor end return %Q{width="#{width.to_i}" height="#{height.to_i}"} end The changes were to not use integer division and put quotes around the numbers in the output tag for proper XHTML. On 10/24/05, Herbert Groot Jebbink <herbert.groot.jebbink-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > Most likely it is just my poor knowlege of Ruby, but the first helper > that I have created does not work how I expected it. > > <%= image_size_tags(@image.width, @image.height) %> > > def image_size_tags (width, height) > > if width > 320 > factor = width / 320 > width = width / factor > height = height / factor > end > > if height > 250 > factor = height / 250 > width = width / factor > height = height / factor > end > > return "width=#{width} height=#{height}" > > end > > it returns the original width and height even if they are bigger then 320 & 250. > > Kind Regards, Herbert > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Any thoughts on the best approach to server a dynamically generated CSV file from a view? I need to serve as either an html table or CSV... I have the table working, but not quite sure how to proceed with the CSV. I''d like the request url to be something like: http://localhost:3000/halfhourreport/get_report_as_csv?ticker=msft and the response contain a CSV file. I have a controller (halfhourreport) that creates the data structure for either report type (csv or html table)... but not sure how to create a view that will have the web server serve up a CSV file (instead of an HTML file). I want the browser to ask the user how they want to view the file (excel).
Lee Marlow wrote:> You might want to try this: >Like Lee said, it was the Integer math killing you, where (639/320 == 1) and (640/320 == 2). One more, slightly normalized, version.. def image_size_tags(width,height) factor = [width.to_f/320.0, height.to_f/250.0, 1.0].max width = width/factor height = height/factor %Q{width="#{width.to_i}" height="#{height.to_i}"} end This just needs some comments and removal of the Magic Numbers. Also, you might consider making your helper unaware of [X]HTML, and leave that completely up to the view. In other words, you could return an array, or hash, instead of an [X]HTML string. --Steve
On 10/24/05, Phil Swenson <phil-XITSOACK58NFw/DY4jzso32qnSAIaJbt@public.gmane.org> wrote:> Any thoughts on the best approach to server a dynamically generated CSV file > from a view? I need to serve as either an html table or CSV... I have the > table working, but not quite sure how to proceed with the CSV.It''s as easy as using send_data in your controller. Jim -- Jim Menard, jim.menard-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org, jimm-Xhj3G7Rj6JI@public.gmane.org http://www.io.com/~jimm