Rails - slice string and rails path - ruby-on-rails

I do some string slice. Right now i have something like this:
#str = '/images00.someother.path_to_.image.jpg' ///my spliced string
When i do this:
#new_string = #str[1..#str.length]
i thought that i will have string like:
'images00.someother.path_to_.image.jpg'
but no... rails put in url path to images, so the output is:
<img src='/images/images00.someother.path_to_.image.jpg'>
when i slice more it looks like 'images/ages01... etc.
How can i remove this default path but only for this action in controller?
VIEW:
<% #array.each do |a| %>
<%= image_tag(a, :id => 'image_' %>

If you mean you don't want the "/images" to appear
just remove the
#new_string = #str[1..#str.length]
if you pass the path with a starting slash rails won't add the default image path
so the output would be
<img src='/images00.someother.path_to_.image.jpg'>
which is the images00.someother.path_to_.image.jpg in your public directory

If you're testing in production mode, rails by default don't serve the static files.
You should change it on config/environments/production.rb the line:
config.serve_static_assets = false
As long as the question is missing some details, it might help :)

Related

Rails: simple asset pipeline issue with images breaking

I have an Item and each item has a string image column where I store the path to an image in assets like assets/images/items/1.jpg
If I call some_item.image, then I'll receive the path to that image like "/assets/items/1.jpg"
If I go into a view and write <img src="/assets/items/1.jpg"> then I will see the proper image. But with image_tag <%= image_tag(some_item.image) %>, the image tag is populated like <img src="images/assets/items/1.jpg"> which does not properly show the image.
How do I fix this?
Instead of image_tag, you should use the image_path() or image_url() helpers instead.
consider the following:
img = 'some_image.png'
image_tag(img) #=> <img src="some_image.png"/>
image_path(img) #=> <img src="/assets/some_image.png"/>
image_url(img) #=> <img src="http://host.ext/assets/some_image.png"/>
I suggest you to define a method on your Item model which return a valid path for image_tag:
Example:
def image_path
self.image_path.sub(/^\/assets\//, '')
# removes '/assets/' from the start of the string
# returns a String like 'items/1.png'
end
in your view:
= image_tag(item.image_path)

Rails 4 link to static file in public in mailer

In a Rails 4 app, I have a static file stored in the /public directory. And I have a view partial that links to that file like this:
<%= "Please download the #{link_to 'Static file', '/static_filename.docx'}".html_safe %>
The partial is used in both a regular view and in a mailer view. In the regular view, this works perfectly, and the link url is like this:
www.example.com/static_filename.docx
But in the mailer, the url comes out like this, missing the host name:
/static_filename.docx
This, despite the fact that I took care to configure the default url in config/environments/production.rb as such:
config.action_mailer.default_url_options = { :host => 'http://www.example.com' }
I'm puzzled as to what I am doing wrong, and why the regular view works when the mailer does not work.
Any help would be greatly appreciated!
You should your asset host for action mailer
config.action_mailer.asset_host = "http://www.yourdomain.com"
Secondly, use the asset_path() wrapper on your asset, ie
<%= "Please download the #{link_to 'Static file', asset_path('/static_filename.docx')}".html_safe %>

Get absolute URL for paperclip attachment

Is it possible to get the absolute URI for a Paperclip attachment? Right now, the problem is that the production environment is deployed in a sub-URI (on Passenger: RackBaseURI), but <paperclip attachment>.url returns the Rails-app relative URI (/system/images/...). Is there a way to get the absolute URI for Paperclip attachments?
I'm using Paperclip v2.7 and Rails 3.2.8.
asset_url(model.attachment_name.url(:style))
Relevant github issue
try
URI.join(request.url, #model.attachment_name.url)
or
URI(request.url) + #model.attachment_name.url
It's safe if you use S3 or absolute url.
Update: this answer is better than mine ;) https://stackoverflow.com/a/21027839/683157
According to this github issue, it is cleaner to use ActionController::Base.asset_host so it would result the helper:
def add_host_prefix(url)
URI.join(ActionController::Base.asset_host, url)
end
This supposes you have in every /config/environments/<environment>.rb file the following:
Appname::Application.configure do
# ....
config.action_controller.asset_host = 'http://localhost:3000' # Locally
# ....
end
The most widely applicable way of doing this is to first define your asset hosts in the relevant config/environment file:
config.action_controller.asset_host = "http://assethost.com"
config.action_mailer.asset_host = "http://assethost.com"
Then in views and mailers:
asset_url(model.attachment.url(:style))
In the console:
helper.asset_url(model.attachment.url(:style))
In a model:
ApplicationController.helpers.asset_url(model.attachment.url(:style))
You can do this:
<%= image_tag "#{request.protocol}#{request.host_with_port}#{#model.attachment_name.url(:attachment_style)}" %>
Or make a helper method to wrap it.
def absolute_attachment_url(attachment_name, attachment_style = :original)
"#{request.protocol}#{request.host_with_port}#{attachment_name.url(attachment_style)}"
end
And use it like this:
<%= image_tag absolute_attachment_url(attachment_name, :attachment_style)}" %>
Ex: Model = Person (#person), attachment_name = avatar, style = :thumb
<%= image_tag absolute_attachment_url(#person.avatar, :thumb)}" %>
This doesn't solve the original poster's problem exactly (it operates in the view, not the model), but may be helpful for people who are trying to "get absolute URL for paperclip attachment" within their view: In the same way that
image_tag(user.avatar.url(:large))
puts the image itself into your view,
image_url(user.avatar.url(:large))
returns just the URL you'll need if you want to link to the asset directly (e.g. in a link_to call).
You can add to your application.rb (or for specific enviroment in config/environments/*):
config.paperclip_defaults = {
url: "http://my.address.com/system/:class/:attachment/:id_partition/:style.:extension",
path: ':rails_root/public/system/:class/:attachment/:id_partition/:style.:extension',
}
Restart and reimport your images.
PS: obviously you can replace http://my.address.com with an environment variable.

Rails 3.1: Trouble on displaying images in mailer view files

I am using Ruby on Rails 3.1 and I would like to add my web site logo (that is, an image handled through the new Asset Pipeline) to an e-mail.
If in my mailer view file I state the following:
<% # Note: '#root_url' is my application hostname (eg: http://www.mysite.com) %>
<%= link_to image_tag( "#{#root_url.to_s}/images/logo.png"), #root_url.to_s %>
it doesn't work in production mode (that is, I cannot display the logo image) because I think the Asset Pipeline uses the Fingerprinting technique and in the received e-mail it doesn't. Inspecting the HTML logo element in the e-mail I get something like this:
<img src="http://www.mysitecom/images/logo.png"> # without Fingerprinting
How can I solve the problem?
In my production.rb file I have the following commented out code:
# Enable serving of images, stylesheets, and javascripts from an asset server
# config.action_controller.asset_host = "http://assets.example.com"
in config/environments/production.rb (and other enviroment files needed) add:
config.action_mailer.asset_host = 'http://mysite.com'
after that rails will automatically add hostname in front of paths generated by image_tag
# haml
= image_tag 'foo.jpg'
will become
#html
<img alt="" src="http://mysite.com/assets/foo.jpg" >
...same apply for image_path
#haml
%table#backgroundTable{background: image_path('email-background.jpg'), width: '100%', :border => "0", :cellpadding => "0", :cellspacing => "0"}
will become
<table background="http://mysite.com/assets/email-background.jpg" border="0" cellpadding="0" cellspacing="0" id="backgroundTable" width="100%">
watch out!!!
# this will make your emails display images
config.action_mailer.asset_host = 'http://mysite.com'
is different than
# this wont make your email images work
config.action_controller.asset_host = "http://mysite.com"
All of these answers are assuming you're using the asset pipeline, but from your example, you're specifying an image in /public/images - this is not part of the asset pipeline, so all the asset_path based answers won't work, and further your initial fingerprinting supposition is incorrect.
If you put an image in /public/images, you want your image tag to have a src of http://yoursite.com/images/the-image.jpeg, no fingerprint, no asset path, nothing - just hard-code it into your view:
<img src="<%=#root_url%>/images/logo.png">
But, you have to actually have the file in that location! If you have your image in /app/assets/images, then you'll need to use image_tag and the asset pipeline as others have answered.
An alternative is to include the image logo in the mail. The mail could also be viewed offline. You can add the logo in you Mailer class, with the following code..
attachments["your_logo.png"] = File.read("#{Rails.root}/assets/images/your_logo.png")
This code will include your image to the mail. I believe when you want to show your attachment in the mail you need to do the following:
Class YourMailer < ActionMailer::Base
def sendmail
.....
attachments.inline['your_logo.png'] = File.read("#{Rails.root}/assets/images/your_logo.png")
end
And in your sendmail.html.erb view you can use the image_tag method:
<%= image_tag attachments['your_logo.png'].url %>
note: if the mail does not get shown correctly you can alternatively try the solution at:
Rails attachments inline are not shown correctly in gmail
Your mail can then also be viewed offline correctly.
Have you tried adding something like this
config.action_mailer.default_url_options = { :host => 'www.example.com' }
to your config/enviroments/production.rb file
Try:
<%= link_to image_tag( "#{#root_url.to_s}/assets/logo.png"), #root_url.to_s %>
You're giving image_tag an absolute url so it thinks it doesn't need to do any fingerprinting or anything else other than regurgitate the string you gave it. I would try
link_to( image_tag('logo.png'), #root_url)
You'll also need to set actioncontroller's asset host to get rails to generate a full url for the image rather than just a path
One caveat to note: if you change the image then the fingerprint will obviously change and so the inage URL in all of your previously sent emails will become invalid. You may wish to consider inline images, although obviously these increase the email size
Try out this one
<%= link_to image_tag( asset_path, 'logo.png'), #root_url.to_s %>
Adding mode:'rb' worked for me:
attachments.inline['Logo.jpg'] = File.read(File.join(Rails.root,'app','assets','images','Logo.jpg'), mode: 'rb')
If you compile your assets:
RAILS_ENV=production bundle exec rake assets:precompile
and use asset_path in your view:
<%= link_to image_tag( asset_path('logo.png') ), #root_url.to_s %>
--it should work in both development and production. This is the way I do it my views, and .css.scss.erb stylesheets. I assume that it doesn't make a difference that it is a view for a mailer.
make sure your html page have follwing header
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
and render image as:
<%= image_tag('http://'+#url+'/images/header.jpg')%>
or
if you want link to image then
<%= link_to image_tag('http://'+#url+'/images/header.jpg'),root_path %>
#url = 'your website address'

modify erb file on deployment

I have a fragment of JavaScript that I want to add to a page, but only in the production environment. Does rails have a way to insert or conditionally include on deployment. I know I could do "if Rails.env.production?" But I'd rather not do this condition check every time the page is loaded.
I wouldn't be worried about the overhead of one if statement.
Why not use a custom helper method:
def snippet
if RAILS_ENV == "production"
javascript_tag "whatever"
elsif . . .
end
then you can use the same syntax:
<%= snippet %>
and you get a couple benefits:
access to other rails helpers
your config file won't be littered with raw html
What I do in this situation is create a constant in each environment's config file:
#config/environments/development.rb
SNIPPET = ""
#config/environments/production.rb
SNIPPET = "<script src='whatever.js'></script>"
#app/views/file.html.erb
<%= SNIPPET %>

Resources