Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7024499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:50:51+00:00 2026-05-27T23:50:51+00:00

I’m working on my first application and I need some help with allowing my

  • 0

I’m working on my first application and I need some help with allowing my users to download a text file with certain variables that are being displayed on the page.

Take a shopping list for example.
Let’s say you allow your users to create a shopping list of products, and then display the shopping list with the items on a shopping list page,

e.g. localhost:3000/list/my-list

Take a look at the example code below (which is probably incorrect):

File.open('shopping_list.txt', 'w') do |file|
  file.puts 'Item 1: #{product_1.name}'
  file.puts 'Item 2: #{product_2.name}'
  file.puts 'Item 3: #{product_3.name}'
end

Which then creates a text file that has the following content:

Item 1: Eggs
Item 2: Butter
Item 3: Bread

Users should then be able to download this file (i don’t want this file to be stored on the server) via a download link.

I have no idea how to achieve this, but I’m hoping you guys can guide me. 😀

TL;DR

  • create text files populated with model data (perhaps create a method to achieve this?)
  • text files should not be stored on the server, but created as users click the download button (not sure if this is the rails way but perhaps someone could show me a better way)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:50:52+00:00Added an answer on May 27, 2026 at 11:50 pm

    I am assuming there is a resource for List with the attribute name as the name of the list and a list has_many Item which has an attribute description

    First off, create a download path change your routes config/routes.rb

    resources :lists do
      member {get "download"}
    end
    

    Now if you run a rake routes in the console you should see a route like

    /lists/:id/download
    

    Whats more you should now have the helpers download_list_url & download_list_path to use in your view like

    <ul>
    <% @lists.each do |list| %>
      <li> <%= list.name %> - <%= link_to 'Download List', download_list_path(list) %> </li>
    <% end %>
    </ul>
    

    In your lists_controller add the action, and as you dont actually want to keep the file on the server disk just stream the data as a string

    def download
      list = List.find(params[:id])
      send_data list.as_file,
        :filename => "#{list.name}.txt",
        :type => "text/plain"
    end
    

    Finally you see I have used a as_file method which you should add to the model (I prefer not to do this stuff in controllers, fat models, skinny controllers). So in the List model

    def as_file
      output = [self.name]
      self.items.each {|item| output << item.description }
      output.join("\n")
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I have a reasonable size flat file database of text documents mostly saved in
In my XML file chapters tag has more chapter tag.i need to display chapters
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.