I am trying to add a download link to a file on my page, the file can either be a Microsoft Word document, a PDF file, or zip file or some other kind of document, i have the path to the file stored in my database, my issue is it works on my localhost but on the server I get: NotFoundHttpException
This is how the link is generated:
<span class="fa fa-download"></span>
This is how the file url looks like:
'uploads/data/library/G7TkXMdk7BAB12Cn//Guide.pdf'
In your problem, try to use helper function that is called link_to_asset.
{{ link_to_asset($row->file_url, "Download", array("class" => "widget-control-right")) }}
But the better approach is here :
<span class="fa fa-download"></span>
In your route.php
Route::get("file/download/{id}", array("as" => "file.download", function ($id) {
$row = Model::find($id);
return Response::download($row->file_url);
}));
Hope it will be useful for you.
Related
is there any way of printing a file on a printer in rails?
Let's assume I have a
def print_url
"/system/tickets/#{print_url_dir}/print.png"
end
and I want to call it with
resource.print_url
I tried already
<%= link_to 'Print', resource.print_url, :onclick => 'window.print();return false;'%>
But that just opened the printer dialog for the site I am on.
Sorry if it is a stupid question or I am missing something.
Thanks and all the best!
If you want to do this from a webpage - it's more of a Javascript task rather than Ruby/Rails one. You can do something like this:
<script type="text/javascript">
var WinPrint = window.open('', '_blank', 'left=0,top=0,toolbar=0,scrollbars=0,status=0');
WinPrint.document.write('<img src="<%= resource.print_url %>" />');
WinPrint.document.close();
WinPrint.focus();
WinPrint.print();
WinPrint.close();
</script>
It creates and open new window with content that contains only the image that would like to print, then print the page (which you already noticed that invokes the dialog) and the closes that new window. (I don't think there's a way to skip the dialog opening though).
You can check this answer for more information and examples.
In my application, I have javascripts, with a refer to default.html. I.e.
function link() {
var url = '<%= asset_path("sidenav/default/default.template.html") %>';
}
This works, but, when I use the same to refer with asset_path in another file, i.e. a html (.html.erb of course, and last script, too is a .js.erb), I receive the error Sprockets::CircularDependencyError. A example of html
<a ng-href="<%= asset_path('sidenav/default/default.template.html') %>">Default</a>
If I dont use the reference to default in javascript, html works, but if I use the same refer in multiples files, this error occur.
Any help?
I have a link like so
= link_to "https://twitter.com/share", class: "twitter-share-button", data: { url: "https://google.com", text: hack.body, via: "GhettoLifeHack_", hashtags: "ghettolifehack" } do
= image_tag "Tweet", alt: "Social Twitter tweet button"
and no matter how much I change the data-url value, the pre-tweet confirmation page always prepopulates the tweet form field with the url of the referring page, not the one I specified. It also ignores my custom data-text as well.
Why is this happening?
I also have this minified script
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
that I got from here https://about.twitter.com/resources/buttons#tweet
Removing that script doesn't seem to change anything.
edit: upon trying using :'data-url' attributes directly, the output html is the same.
I am testing hardcoded strings and dynamically generated urls at the same time. The first is the dynamic one.
<a class="twitter-share-button" href="https://twitter.com/share" data-via="GhettoLifeHack_" data-url="http://localhost:3000/hacks/1" data-text="asdf comment body" data-hashtags="ghettolifehack">
<img src="/images/Tweet" alt="Tweet" title=""></img>
</a>
The second is the hard coded strings
<a class="twitter-share-button" href="https://twitter.com/share" data-via="GhettoLifeHack_" data-url="httpL//google.com" data-text="custom text" data-hashtags="ghettolifehack">
<img src="/images/Tweet" alt="Tweet" title=""></img>
</a>
I've tested on development and in production. Both have the same behavior of pre-populating the tweet form with the referring url, rather than the specified url and text.
This works in Chrome for me but not in Firefox 32
The code provided by you is perfectly fine and should work as expected.
Many site issues can be caused by corrupt cookies or cache. Try to clear both cookies and the cache. I would suggest you to look into the following link to see why it is not working in firefox
The issue was specific to firefox browser. I'm not sure what addon or setting is causing the conflicts, but it is working perfectly find in chrome, including the popup window.
In my Rails app, I am using a img tag in Application_helper.rb, But I think Assests pipeline is not working here, So the image is not loading on the page. And Browser console show as error of "failed to load the given URL".
One line fro my code block is as :
text = "<input type='checkbox'/><img src='Icon_Bar.png' />#{r[:value]}<span class='small'>(#{r[:count]})</span>".html_safe
And it works fine, when I used any online path for the image. Please Help.
Try this,
text = "<input type='checkbox'/><img src='/assets/Icon_Bar.png' />#{r[:value]}<span class='small'>(#{r[:count]})</span>".html_safe
Add /assets/ before the image.extension.
when Icon_Bar.png in assets/images/Icon_Bar.png
Try:
<img src="<%= image_path('Icon_Bar.png') %>" />
The doc is here: http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html#method-i-image_path
Try :
<%= image_tag("Icon_Bar.png") %>
Watch out for the capital letters , they also matter in referencing to assets.
When trying to include a php file into another it either does nothing at all (no errors). If i require it will give a useless message
"Failed opening required 'http://example.org.com/xxx.php' (include_path='.:/usr/local/php5/lib/php')
<div id="menu"> <?php include("http://www.sitename.com/menu.php") ?> </div>
The file im trying to include is on the same site and host.
The page im including from is:
root/wiki/skin/index.php (mediawiki)
menu.php is located:
root/menu.php
since mediawiki treats the /wiki/ path as the root i cannot back out with ../../ etc.
What am i doing wrong here? Thanks
Sir, try this:
<div id="menu"> <?php include("../../menu.php") ?> </div>
To include URLs in your PHP project you must have enabled (true) this option:
allow_url_include
in your php.ini configuration. More information you can find here:
http://php.net/manual/en/filesystem.configuration.php#ini.allow-url-include