Hello, I have been playing with this tutorial: http://wiki.rubyonrails.com/rails/show/HowtoUploadFiles I can upload image to a database and render it through the controler with: def render_image @document=Resource.find(@params["id"]) send_data decode64( @document.picture ), :filename => @document.filename, :type => "image/jpeg", :disposition => "inline" end However, I would like to display an image in the .rhtml files, i.e. while listing the contents of my database. I try to roll something like: <% for resource in @resources %> <img src="<%= resource.as_image %>"> <% end %> With this code in the model: def as_image send_data decode64( self.picture ), :filename => self.filename, :type => "image/jpeg", :disposition => "inline" end I get simple error: undefined method `send_data''. But when I require ''streaming'' form the resource.rb (the model), I get another errors. Resolving them will probably lead to require''ing other parts of ActionController... but this looks like messing the MVC model! I just want to display my images on the page ;-) Hope someone can point me in the right direction. - Tom
What you're trying to do would actually result in the page containing something like: <img src="lots_of_binary_data_instead_of_a_filename"> You should actually create a seperate action that *just* returns the image data, with the appropriate HTTP header, and then specify the image source as being the result of that action, so it would be rendered as: <img src="/controller/action/someid" /> I don't know if there's a Rails helper to create the <img /> tag based on Routes definitions, but its worth looking. - chris On 9/16/05, Tomasz Bąk <openbsdpl@wp.pl> wrote:> Hello, > I have been playing with this tutorial: > http://wiki.rubyonrails.com/rails/show/HowtoUploadFiles > > I can upload image to a database and render it > through the controler with: > > def render_image > @document=Resource.find(@params["id"]) > send_data decode64( @document.picture ), > :filename => @document.filename, > :type => "image/jpeg", > :disposition => "inline" > end > > However, I would like to display an image in the .rhtml > files, i.e. while listing the contents of my database. > I try to roll something like: > > <% for resource in @resources %> > <img src="<%= resource.as_image %>"> > <% end %> > > With this code in the model: > def as_image > send_data decode64( self.picture ), :filename => self.filename, :type > => "image/jpeg", :disposition => "inline" > end > > I get simple error: undefined method `send_data'. > But when I require 'streaming' form the resource.rb > (the model), I get another errors. Resolving them > will probably lead to require'ing other parts of > ActionController... but this looks like messing the > MVC model! > > I just want to display my images on the page ;-) > Hope someone can point me in the right direction. > > - Tom > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Chris Lambert wrote:> What you''re trying to do would actually result in the page containing > something like: > <img src="lots_of_binary_data_instead_of_a_filename"> > > You should actually create a seperate action that *just* returns the > image data, with the appropriate HTTP header, and then specify the > image source as being the result of that action, so it would be > rendered as: > <img src="/controller/action/someid" />That does the trick.> > I don''t know if there''s a Rails helper to create the <img /> tag based > on Routes definitions, but its worth looking.I have found only image_tag, but it appends ".png" if file name is without extension. ;-( Best regards - Tom
Hey group, I have a query that I''m using find_by_sql to get the results for my application. SQL Code: select b.naics_industry, n.naicstitle, b.total_input, c.local_input, c.local_input/b.total_input as percent_local from NAICS n inner join (select naics_industry, sum(percent_input) as total_input from Inputs group by naics_industry) b on n.naicscode = b.naics_industry inner join (select io.naics_industry, sum(io.percent_input) as local_input from Inputs io inner join temp_local_naics ln on io.naics_industry = ln.naics inner join (select distinct naics_commodity from Inputs io inner join temp_local_naics ln on io.naics_commodity = ln.naics) d on io.naics_commodity = d.naics_commodity where user_id = ''9'' group by io.naics_industry) c on n.naicscode = c.naics_industry order by (c.local_input/b.total_input)+b.total_input desc It has a lot of joins, I know, but when I run it on my database through PhpMyAdmin, it runs in 0.3585 seconds, but when I run it through my Rails app it takes 10 seconds or more to run and return the results. This query results in only 29 records. I should note that this is the speed on my development computer running Rails using FastCGI. I haven''t tested it on my live server yet, which I will later today, but I would like to know if there''s something else I can do to speed this up. Thanks. - Clint
Does this query actually need to return an instance of one of your models, or do you only need the column values? If you only need the results you''d see by executing that query in your admin tool, you can do: query = "select all that SQL code and whatnot from somewhere" @matches = ActiveRecord::Base.connection.select_all(query) That will return an array of hashes, with keys named based on the columns returned. @matches[0] is the first result row, @matches[1] is the second, etc. Because the "outermost" structure is an array, it preserves the order of your results. On 9/16/05, Clint Pidlubny <clint-DOvxo+vduAZWk0Htik3J/w@public.gmane.org> wrote:> Hey group, > > I have a query that I''m using find_by_sql to get the results for my > application. > > SQL Code: > select b.naics_industry, n.naicstitle, b.total_input, c.local_input, > c.local_input/b.total_input as percent_local > from NAICS n > inner join > (select naics_industry, sum(percent_input) as total_input > from Inputs > group by naics_industry) b > on n.naicscode = b.naics_industry > inner join > (select io.naics_industry, sum(io.percent_input) as local_input > from Inputs io > inner join temp_local_naics ln > on io.naics_industry = ln.naics > inner join > (select distinct naics_commodity > from Inputs io > inner join temp_local_naics ln > on io.naics_commodity = ln.naics) d > on io.naics_commodity = d.naics_commodity > where user_id = ''9'' > group by io.naics_industry) c > on n.naicscode = c.naics_industry > order by (c.local_input/b.total_input)+b.total_input desc > > It has a lot of joins, I know, but when I run it on my database through > PhpMyAdmin, it runs in 0.3585 seconds, but when I run it through my > Rails app it takes 10 seconds or more to run and return the results. > This query results in only 29 records. I should note that this is the > speed on my development computer running Rails using FastCGI. I haven''t > tested it on my live server yet, which I will later today, but I would > like to know if there''s something else I can do to speed this up. > Thanks. > > - Clint >
I could do this. Are you saying this will be faster? I can''t test it till this evening. If it is faster, do you know why? I''m still learning, so I like to understand the why''s as much as the how''s. Thanks for the help. Wilson Bilkovich wrote:>Does this query actually need to return an instance of one of your >models, or do you only need the column values? >If you only need the results you''d see by executing that query in your >admin tool, you can do: >query = "select all that SQL code and whatnot from somewhere" >@matches = ActiveRecord::Base.connection.select_all(query) >That will return an array of hashes, with keys named based on the >columns returned. >@matches[0] is the first result row, @matches[1] is the second, etc. >Because the "outermost" structure is an array, it preserves the order >of your results. > >On 9/16/05, Clint Pidlubny <clint-DOvxo+vduAZWk0Htik3J/w@public.gmane.org> wrote: > > >>Hey group, >> >>I have a query that I''m using find_by_sql to get the results for my >>application. >> >>SQL Code: >>select b.naics_industry, n.naicstitle, b.total_input, c.local_input, >> c.local_input/b.total_input as percent_local >>from NAICS n >>inner join >>(select naics_industry, sum(percent_input) as total_input >>from Inputs >>group by naics_industry) b >> on n.naicscode = b.naics_industry >>inner join >>(select io.naics_industry, sum(io.percent_input) as local_input >>from Inputs io >>inner join temp_local_naics ln >> on io.naics_industry = ln.naics >>inner join >>(select distinct naics_commodity >>from Inputs io >>inner join temp_local_naics ln >> on io.naics_commodity = ln.naics) d >> on io.naics_commodity = d.naics_commodity >>where user_id = ''9'' >>group by io.naics_industry) c >> on n.naicscode = c.naics_industry >>order by (c.local_input/b.total_input)+b.total_input desc >> >>It has a lot of joins, I know, but when I run it on my database through >>PhpMyAdmin, it runs in 0.3585 seconds, but when I run it through my >>Rails app it takes 10 seconds or more to run and return the results. >>This query results in only 29 records. I should note that this is the >>speed on my development computer running Rails using FastCGI. I haven''t >>tested it on my live server yet, which I will later today, but I would >>like to know if there''s something else I can do to speed this up. >>Thanks. >> >>- Clint >> >> >> >_______________________________________________ >Rails mailing list >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >http://lists.rubyonrails.org/mailman/listinfo/rails > > > >-- Clint Pidlubny clint-DOvxo+vduAZWk0Htik3J/w@public.gmane.org 612.590.8343
Try something like this: <%= tag(''img'',:src => url_for(:action => ''show'', :id => image), :border => 0) %> CHEERS> SAM Tomasz Bąk wrote:> I have found only image_tag, but it appends ".png" > if file name is without extension. ;-( > > Best regards > - Tom > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >
[@matches = ActiveRecord::Base.connection.select_all(query)] On Friday 16 September 2005 20:31, Clint Pidlubny wrote:> I could do this. Are you saying this will be faster? I can''t test it > till this evening. If it is faster, do you know why? I''m still > learning, so I like to understand the why''s as much as the how''s.If you need raw speed and are using PostgreSQL, you could do @matches = ActiveRecord::Base.connection.query(query) It returns rows as arrays. I don''t know whether the other adapters have a similar method. Michael -- Michael Schuerig Life is what happens mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org While you''re making plans http://www.schuerig.de/michael/ --Kevin Gilbert, A Long Day''s Life