Enabling users to download current data on the page - ruby-on-rails

I have a page that lists records based on the parameters given in the search filter. I need to give a download link, wherein the current records on the page are written to a file and given as a link to be downloaded.
So to put it simple, how do I give the latest records found based on the search parameters as a download link ?
P.S : I'm using the send_file method.

You could link to the download action using a custom :format merged with the query params. Something like this:
<%= link_to "Download", posts_path(params.except("action", "controller").merge(:format => "csv")) %>
Not the cleanest example but hopefully you get the idea.

You can use CSV writer to generate the required file and give a download link to the file for users.
Thanks, Anubhaw

Related

How to fix rails 4 hyperlinks?

I'm trying to display someone's profile links to their social media profiles. My current code is
<p>
<strong>Linkedin:</strong>
<%= link_to #person.linkedin, #person.linkedin %>
</p>
It works and the link does load, but it goes to localhost:3000/user/linkedin.com/in/user instead of linkedin.com/in/username
Thanks!
You need to add make sure the linkedin hyperlinks have a protocol (http, https, etc).
Any link without a protocol is assumed to be a relative path, which is why the hyperlinks are getting appended to your website url.
A solution would be to manually add a "http://" string at the beginning of every person's linkedin hyperlink in your database. Your code should work fine after that.
Edit: Or you can change it on the fly like so (the other answers won't work since it looks like #person.linkedin contains the entire hyperlink not just the linkedin user)
<%= link_to #person.linkedin, "https://#{#person.linkedin}" %>
Rails link helpers follow the format:
link_to(name = nil, options = nil, html_options = nil, &block)
The second #person.linkedin path is a local path as determined by your routes file in your config folder. If the link you need follows a certain format you can do something like
<%= link_to "LinkTextHere", "http://www.linkedin.com/#{#person}/profile" %>
I can answer in more detail if you give me the exact outcome you need as well as what you want from the .linkedin value. Also, typing "rake routes" in your console will show all paths you currently have and can help troubleshoot issues like why #person.linkedin is routing locally.

Create Link to External Website

I am trying to create an external link to each individual listing's assigned website address. Using the following code: (The listing website is saved as google.com)
External Link
Takes me to:
localhost:3000/google.com
Is there any way to generate a link that would go to www.google.com instead of trying to find a route in my application.
The reason why it's bringing you to localhost:3000/google.com it's probably because the string you are passing to the href attribute is not a full qualified URL.
In fact, if in HTML you write
External Link
The string will be appended to the current page path. You should make sure that the input you pass always contains the schema. If the input never contains that, then you can assume it's http://
External Link
But this is not really a solution, just a workaround. You should definitely make sure that when you populate the website URL, you store a complete URL. In fact, some sites may require https.
In Rails you normally use the url_for and link_to helpers to generate an URL, but they will both cause the same issue unless you pass a full URL.
<%= link_to "External Link", "http://#{listing.website}" %>
Do it the Rails way:
<%= link_to 'External Link', "http://#{listing.website}" %>
You need to put in the protocol.
Google
Do you get it? =)
You can create link like this:
External Link

is it possible to show only a part of the image URL in rails?

I am using Carrierwave and S3 to allow people to upload images and the image URL is displayed as "https://myapp.s3.amazonaws.com/image/53/98ccfeca46.jpg".
I would like to do one of two things.
I would like to show just
"/image/53/98ccfeca46.jpg"
or 2. Use CName to rename the URL to not show "s3.amazonaws.com" so that the image URL will be
https://myapp.com/image/53/98ccfeca46.jpg
How can I achieve these? Is it possible to cut the image URL right after .com?
I am using <%= link_to xyz.image % > to display the URL.
You could create a helper method for displaying the url, and use string sub to cut off the first part of the url.
So in your helpers folder, in the helper for the corresponding model, you might have a function like
def show_url(url)
return url.sub("https://myapp.s3.amazonaws.com", "")
end
And then in your helper you would have <%= link_to show_url(xyz.image) %>
Or indeed something similar, but using regular expressions
def show_url(url)
return url.match(/.com(.+)/)[1]
end
If you're using carrierwave, you can call .path to retrieve the path of the image. I tried this locally and it worked fine but I'm not sure if it will work in production. Try this:
<%= link_to xyz.image.path %>
Alternatively, you can use CloudFront to server you S3 files, which allows you add a bunch of custom CNAMEs to your distribution, so you can have assets.myapp.com/xyz.jpg

good tutorial on how to use auto_html filters for rails?

I know it's supposed to be easy, but I just can't figure out how to modify an existing auto_html filter to get the output I need. I found this Creating filters for auto_html
but I need to modify an existing filter.
for example, I have a list of youtube links in the db in this format (http://youtu.be/UfQC1h-EANI) and printed out in my view:
%li
= link_to link.title, link.url, :class => "youtube title_link"
When the the link is clicked, I need the http://youtu.be/UfQC1h-EANI converted to http://www.youtube.com/embed/UfQC1h-EANI?rel=0
I've managed to get a conversion working using auto_html, however, it's coming with all of the html attached to it. I'm thinking if I can modify the filter, I can adjust it to just give me the url without all the html. I can't for the life of me figure out how to modify auto_html's youtube filter. Is this even the best approach for this?
Any help is muchly appreciated!
Just do it with regex
link_to link.title, "http://youtube.com/embed/#{link.url.to_s.match(/\/\/youtu.be\/(\S+)$/)[1]}/?rel=0", :class => "youtube title_link"
If you want it onclick only, you can do it in JavaScript.

get the seo-unfriendly version of a url

I'm adding disqus commenting to some articles on our site and all URLs are SEO friendly.
This means that, if the title of the article changes so will the URL of that article, which will discard the previous disqus comments (linked to the previous version of the URL).
The solution would be to strip out the title of the article from the URL before passing it to Disqus.
So I need to turn "http://mydomain.com/article/123-myarticle/section/1-sectiontitle" into "http://mydomain.com/article/123/section/1"
What is the easiest way to do this?
Thanks!
PS: I'm very new to Rails (i'm taking over a developed project)
You don't need to extract anything from the URL.
All you need to give to Disqus is a unique id.
So you can add a method to your model, called disqus_id for instance:
def disqus_id
"name_of_your_model_#{id}"
end
and then, in the javascript:
disqus_identifier = "<%= #your_model.disqus_id %>";

Resources