B_.
B_.

Reputation: 2254

How to generate and link to an arbitrary file?

Here's what I'm trying to do. Let's say the user is looking at the foo view for the foo action of the bar controller, and I've got a variable called @userName.

bar_controller.rb

class BarController
  def foo
    @userName = getUserName();
  end

foo.html.erb

Hi mom!

I want to create a filed called <%= @userName %>.myExt with the information Hi, I'm <%= @userName %>! in it and put a link to it in the view. How do I do this?

i.e. final:

bar_controller.rb

def foo
  @userName = getUserName();
  create_myExt_file(@userName);

foo.html.erb

Hi mom! Click <%= generate link to @userName.myExt, "here" %> to view!

<@userName>.myExt

Hi, I'm <@userName>!

Ideally the @userName.myExt file doesn't have to actually be written to the hard drive, but could be created from a template or something. I don't know how to do this!

Thanks!

Upvotes: 1

Views: 70

Answers (1)

Peter
Peter

Reputation: 132227

First, generate the file as a string, such as:

s = get_file_contents

Then, in your controller, send it to the client, along with a suggested filename:

send_data s, :filename => 'example.text'

Finally, to use an ERB template, you can just render_to_string.

Upvotes: 1

Related Questions