1

in my show route, there is a my_search route (basically show#my_search) that shows an array of hashes of data in HTML.

What I need to do is just dump @data into my render (or partial) and deal with them in views, making it a HTML table with embedded ruby.

However, is there a easy way to send the same @data to a CSV file? Do I have to get the @data again and make another route specially for it? Is it a way to access the @data (preferably, in a link to download its CSV or json render) when showing the page localhost://show/my_search?

Edit:

The @data looks like:

@data = [ {"name"=>"John", "age"=>"21"}, {"name"=>"Amy", "age"=>"20"} ]

The app/controllers/show_controller.rb looks like:

def my_search
    @data = [ {"name"=>"John", "age"=>"21"}, {"name"=>"Amy", "age"=>"20"} ] # and other rows
    # and do other stuff here

    render "table_template"

in app/views/show/table_template.html looks like:

<table>
  <thead>
  <tr>
    <th>Name</th>
    <th>Age</th>
  </tr>
  </thead>

  <tbody>
  <% @data.each do |row| %>
      <tr>
        <td><%= row['name'] %></td>
        <td><%= row['age'] %></td>
      </tr>
  <% end %>
  </tbody>
</table>

Update 6/20/2016: my current workround:

app/controllers/show_controller.rb:

def my_search
     get_data
    # and do other stuff here

    render "table_template"
end

def my_search_export
    get_data 
    format.each do |format|
    # something that renders the CSV format when visiting localhost://my_search_export.csv
        .....
    end
end




private
def get_data # writes to @data
    @data=[ {"name"=>"John", "age"=>"21"}, {"name"=>"Amy", "age"=>"20"} ]
end

in view: add a url to localhost://my_search_export.csv.

Bad thing is it loads the data again, good thing is the workflow is simple. I am still looking for a better solution.

P.s. this website may have different user running at the same time so keeping a global variable doesn't sound right for me.

Sean L
  • 151
  • 2
  • 12
  • Possible duplicate of [Output array to CSV in Ruby](http://stackoverflow.com/questions/4822422/output-array-to-csv-in-ruby) – Seth Jun 16 '16 at 00:30
  • Thanks Seth, but how I output the CSV downloadable link when showing a webpage of `localhost://show/my_search` – Sean L Jun 16 '16 at 18:07
  • http://stackoverflow.com/questions/94502/in-rails-how-to-return-records-as-a-csv-file may help -- but this will only allow going to `localhosts://some/path/file.csv` which means it shows no webpage in `localhosts://some/path/file.csv` – Sean L Jun 16 '16 at 18:09

2 Answers2

2

You can open up a CSV and write to it before your render if you want:

@data = { first_stuff: ['a', 'b', 'c'], second_stuff: [1, 2, 3] }
CSV.open('some/file.csv', 'w') do |csv|
  @data.each_pair do |key, value|
    csv << value
  end
end
render json: @data

... and so forth. It's hard to get more specific without knowing what your @data looks like, but I hope that helps!

jmschles
  • 274
  • 1
  • 6
  • Thanks for helping! I add more description to the question. Is it that if this function has `render json: @data` then it will not render any html page? Is it a way to access the `@data` when showing the page `localhost://show/my_search`? – Sean L Jun 16 '16 at 18:01
  • Oh, do you mean to write CSV file (and probably add a link to the path of that `some/file.csv` in the view) to local path (maybe in `public` folder?) before render the webpage? – Sean L Jun 16 '16 at 18:06
  • Oh, if you're you're rendering an HTML page, your controller method's instance variables should simply be available in the view. In other words, just render the page normally and `@data` should be usable in the template. As for your second comment, you could set the CSV's file path to an instance variable (i.e. `@csv_path = 'some/file.csv'`) and then it, too would be available in your view for linking purposes. – jmschles Jun 16 '16 at 19:08
  • Thanks! So I will just write the CSV to a local copy of `'some/file.csv'` and then link this to the user? That will be a good workaround, but I wonder if it can gives one-time download link and not storing much thing in the website.. @jmschles – Sean L Jun 20 '16 at 21:49
0

Update 6/20/2016: my current workround:

app/controllers/show_controller.rb:

def my_search get_data # and do other stuff here

render "table_template"

end

def my_search_export get_data format.each do |format| # something that renders the CSV format when visiting localhost://my_search_export.csv ..... end end

private def get_data # writes to @data @data=[ {"name"=>"John", "age"=>"21"}, {"name"=>"Amy", "age"=>"20"} ] end in view: add a url to localhost://my_search_export.csv.

Bad thing is it loads the data again, good thing is the workflow is simple. I am still looking for a better solution.

P.s. this website may have different user running at the same time so keeping a global variable doesn't sound right for me.

Sean L
  • 151
  • 2
  • 12