Forum: Ruby on Rails Images with fingerprinting

6dc2ee98385cd0c8372988e8c9a91fe6?d=identicon&s=25 Nick Khamis (Guest)
on 2013-08-08 21:09
(Received via mailing list)
Looking through the asset pipeline documentation, I was able to make
everything
work as expected. Really nice!! However, not for images that we are
loading
using
a variable (i.e., wine.picture) which basically resolves to
(bold_nine.jpg). First
question, is it possible to use (asset_path) and pass variables?

From the little bit of searching I found that (asset_path) does not
support
variables,
and we should use (asset_url) instead. My concern is production env and
the
fingerprinting
of images. Some observation:

$('#pic').attr('src', "<%= asset_url('block_nine.jpg')%>")
------correctly
yields----->  $('#pic').attr('src',
"/assets/block_nine-b3cebc2ed66a8605caae943eacdd358d.jpg")
$('#pic').attr('src', "<%= asset_url('" + wine.pictures + "')%>")
---------incorrectly yields-------> $('#pic').attr('src', "/" + wine.
pictures + "")

Error in console:

ActionController::RoutingError (No route matches [GET]
"/block_nine.jpg")

That being said,

$('#pic').attr('src', "<%= asset_url('assets/" + wine.picture + "')%>");
$('#pic').attr('src', "<%= asset_path('assets/" + wine.picture +
"')%>");

both work in development and production however, I think at that point I
am
serving up the images from
assets and not the web server using the fingerprints as intended.

Your help is greatly appreciate,

Nick.
Aa082c8b00a50928e5860dcd70bf2368?d=identicon&s=25 tamouse m. (tamouse_m)
on 2013-08-09 17:21
(Received via mailing list)
On Aug 8, 2013, at 2:08 PM, Nick Khamis <symack@gmail.com> wrote:

> $('#pic').attr('src', "<%= asset_url('" + wine.pictures + "')%>")
---------incorrectly yields-------> $('#pic').attr('src', "/" + wine.pictures +
"")
>

can't tell for sure here - but it seems you might have the " and '
reversed in the above?

you have the exterior quotes around the <%=..%> so you just need the
string value produced by the asset_url, so try:

$('#pic').attr('src', "<%= asset_url(wine.pictures)%>")
6dc2ee98385cd0c8372988e8c9a91fe6?d=identicon&s=25 Nick Khamis (Guest)
on 2013-08-22 23:16
(Received via mailing list)
Hello Tamouse,

Thank you so much for your reply,

Where trying $('#pic').attr('src', "<%= asset_path(wine.picture)%>");, I
get the following error:

undefined local variable or method `wine' for
#<#<Class:0x4231bdb0>:0x4311bb04>

I hope I did not forget to mention that. wine.picture is a dynamic
variable.

Thanks in Advance,

Nick.
Aa082c8b00a50928e5860dcd70bf2368?d=identicon&s=25 tamouse m. (tamouse_m)
on 2013-08-22 23:52
(Received via mailing list)
On Aug 22, 2013, at 4:15 PM, Nick Khamis <symack@gmail.com> wrote:

> Hello Tamouse,
>
> Thank you so much for your reply,
>
> Where trying $('#pic').attr('src', "<%= asset_path(wine.picture)%>");, I get the
following error:
>
> undefined local variable or method `wine' for #<#<Class:0x4231bdb0>:0x4311bb04>
>
> I hope I did not forget to mention that. wine.picture is a dynamic variable.

It appears that wine is not defined in the scope of that ERB call. How
are you passing it to your view?
6dc2ee98385cd0c8372988e8c9a91fe6?d=identicon&s=25 Nick Khamis (Guest)
on 2013-08-23 00:29
(Received via mailing list)
Tamouse,

I really appreciate this, and apologize for the partial code...

`wine` is the parameter of a render function:

function renderDetails(wine) { $('#wineId').val(wine.id);
$('#name').val(wine.name); $('#grapes').val(wine.grapes);
$('#country').val(wine.country); $('#region').val(wine.region);
$('#year').val(wine.year); $('#pic').attr('src', wine.picture);
$('#description').val(wine.description); }

During runtime, the variable is filled with this JSON:


   1. wine: Object
      1. country: "France"
      2. description: "The aromas of fruit and......"
      3. grapes: "Grenache / Syrah"
      4. id: 1
      5. name: "CHATEAU DE SAINT COSME"
      6. picture: "saint_cosme.jpg"
      7. region: "Southern Rhone / Gigondas"
      8. year: "2009"
   2. PS I am new to Rails, so please forgive the noob :)

Thanks in Advance,

Nick.
Aa082c8b00a50928e5860dcd70bf2368?d=identicon&s=25 tamouse m. (tamouse_m)
on 2013-08-24 02:16
(Received via mailing list)
On Aug 22, 2013, at 5:29 PM, Nick Khamis <symack@gmail.com> wrote:
>   $('#description').val(wine.description);
>      name: "CHATEAU DE SAINT COSME"
>      picture: "saint_cosme.jpg"
>      region: "Southern Rhone / Gigondas"
>      year: "2009"
>    PS I am new to Rails, so please forgive the noob :)
> Thanks in Advance,

So wine is actually a Javascript object? From what I'm seeing above, it
is in your Javascript, and you don't have access to it inside an ERB
call.

So way back at the beginning, you were having trouble with this:

$('#pic').attr('src', "<%= asset_url('" + wine.pictures + "')%>")

when in fact, there shouldn't have been any ERB stuff there at all:

$('#pic').attr('src', wine.pictures)

like you have in the above renderDetails function. It sounds now like
you are setting that via an AJAX call. Where you need to look is in the
controller that is responding to that AJAX call in your Rails
application, and look how it is determining the value it will send for
the picture source url. That is likely where you need to use asset_url
(without any ERB stuff) to set that part of the JSON payload that gets
returned to the client.
6dc2ee98385cd0c8372988e8c9a91fe6?d=identicon&s=25 Nick Khamis (Guest)
on 2013-08-27 19:26
(Received via mailing list)
Hello Tamouse,


>> It sounds now like you are setting that via an AJAX call.

This is correct. We are using JQuery to manage the payload.

>> Where you need to look is in the controller that is responding to that
AJAX call in your Rails application,
>> and look how it is determining the value it will send for the picture
source url. That is likely where you
>> need to use asset_url (without any ERB stuff) to set that part of the
JSON payload that gets returned
>> to the client.

I know this makes total sense, the problem is I am too new to Rails (not
the case for C, C++ or Lisp) to
understand :). The value for wine.picture url is coming straight from
the
database:

+----+----------------------------------------------+------+----------------+---------------------------------------+
| id | name                                       | year | country     |
picture                              |
+----+----------------------------------------------+------+----------------+---------------------------------------+
|  1 | CHATEAU DE SAINT COSME    | 2009 | France    | saint_cosme.jpg
         |

If I understand you correctly, I would need to modify the picture field
in
the database to
read asset_url('saint_cosme.jpg') without any of the ERB stuff "<%=
%>"?

Or if by controller you mean rails controller in the traditional sense,
I
have a very simple
`pages_controller.rb` that contains the following:

class PagesController < ApplicationController
  def index
  end
end

As usual, forgive the noob, and thanks in advance!!!

Nick.
Aa082c8b00a50928e5860dcd70bf2368?d=identicon&s=25 tamouse m. (tamouse_m)
on 2013-08-29 18:53
(Received via mailing list)
On Aug 27, 2013, at 12:25 PM, Nick Khamis <symack@gmail.com> wrote:

> >> to the client.
> read asset_url('saint_cosme.jpg') without any of the ERB stuff "<%=    %>"?
Do not modify the value in your database.

> Or if by controller you mean rails controller in the traditional sense, I have a
very simple
> `pages_controller.rb` that contains the following:

I mean the controller that is being called by your AJAX request. It is
entirely possible this in not your Rails app, but I would think it would
be. You have to look at your javascript and find the AJAX call, then
look at the url in that call to find the route it's seeking, and match
that route to the controller. You can find out which routes map to
controllers by running `rake routes`.
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2013-08-29 18:57
(Received via mailing list)
On 29 August 2013 17:51, Tamara Temple <tamouse.lists@gmail.com> wrote:
>> >> Where you need to look is in the controller that is responding to that AJAX
call in your Rails application,
>> |  1 | CHATEAU DE SAINT COSME    | 2009 | France    | saint_cosme.jpg
|
>>
>> If I understand you correctly, I would need to modify the picture field in the
database to
>> read asset_url('saint_cosme.jpg') without any of the ERB stuff "<%=    %>"?
>
> Do not modify the value in your database.
>
>> Or if by controller you mean rails controller in the traditional sense, I have
a very simple
>> `pages_controller.rb` that contains the following:
>
> I mean the controller that is being called by your AJAX request. It is entirely
possible this in not your Rails app, but I would think it would be. You have to
look at your javascript and find the AJAX call, then look at the url in that 
call
to find the route it's seeking, and match that route to the controller. You can
find out which routes map to controllers by running `rake routes`.

Also you can look in log/development.log to see what, if any, action
is being called in the rails app when you perform the action.

Colin
6dc2ee98385cd0c8372988e8c9a91fe6?d=identicon&s=25 Nick Khamis (Guest)
on 2013-10-01 04:23
(Received via mailing list)
Hello Guys,

Sorry for the delayed response, I had to focus my effort elsewhere
however,
really need to figure (no pun intended) this out now.
When looking into development.log, I see the following error:

I, [2013-09-30T22:16:11.213141 #12076]  INFO -- : Started GET
"/pages/index" for 127.0.0.1 at 2013-09-30 22:16:11 -0400
I, [2013-09-30T22:16:11.221511 #12076]  INFO -- : Processing by
PagesController#index as HTML
I, [2013-09-30T22:16:11.226746 #12076]  INFO -- :   Rendered
pages/index.html.erb (0.6ms)
I, [2013-09-30T22:16:11.229833 #12076]  INFO -- : Completed 200 OK in
5ms
(Views: 4.2ms | ActiveRecord: 0.0ms)
I, [2013-09-30T22:16:12.990109 #12076]  INFO -- : Started GET
"/block_nine.jpg" for 127.0.0.1 at 2013-09-30 22:16:12 -0400
F, [2013-09-30T22:16:12.995561 #12076] FATAL -- :
ActionController::RoutingError (No route matches [GET]
"/block_nine.jpg"):

Which is understandable since the file in the webserver is:

block_nine-dbda8c968180776a036973cf045baba1.jpg

The controller is not taking into consideration the fingerprint added by
the precompile process. Please help!

Thanks in Advance,

Nick.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.