Send_file errors out with nil.update

Hey everyone,

I’ve got a basic setup via an apache2 server, virtualhost ect. At the
moment I’m just trying to use the send_file function of rails
(actionpack I believe?) but it continues to error out. Here is the
breakdown:

#Controller#

class Misc < ApplicationController

def stuff
send_file("#{RAILS_ROOT}/downloads/test.txt")
end

end

#View#

<% a = Misc.new %>

<%= a.stuff %>

And this is the error I’ve been getting:

You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.update

Extracted source (around line #3):

2: <% a = Misc.new %>
3:

<%= a.stuff %>

Trace yields the following:

vendor/rails/actionpack/lib/action_controller/streaming.rb:139:in
send_file_headers!' vendor/rails/actionpack/lib/action_controller/streaming.rb:72:insend_file’
app/controllers/home_controller.rb:16:in stuff' app/views/home/index.html.erb:3:in_run_erb_47app47views47home47index46html46erb’

Any suggestions?

one thing i will note, dunno if it matters, is that rails 2.3.5 wouldnt
install - kept complaining about rack 1.0.0 runtime, so instead i
installed all the packages and dependencies separately via apt-get
install actionpack -v=2.3.4
ect ect, using 2.3.4 instead

Thanks!

-Regards
Mac

On Jan 15, 6:15Â am, Michael L. [email protected] wrote:

 def stuff
  send_file("#{RAILS_ROOT}/downloads/test.txt")
 end

end

#View#

<% a = Misc.new %>

<%= a.stuff %>

This looks a little weird - from inside the view you’re trying to
create a new instance of a controller ? This is probably failing
because you haven’t initialized the controller properly. Why not call
send_file from the appropriate controller action ?

Fred

Frederick C. wrote:

On Jan 15, 6:15Â am, Michael L. [email protected] wrote:

 def stuff
  send_file("#{RAILS_ROOT}/downloads/test.txt")
 end

end

#View#

<% a = Misc.new %>

<%= a.stuff %>

This looks a little weird - from inside the view you’re trying to
create a new instance of a controller ? This is probably failing
because you haven’t initialized the controller properly. Why not call
send_file from the appropriate controller action ?

Fred

o_O worked without a hitch… why would this be? As you suggested I
threw it back in HomeController (via script/generate controller home
index) under def index and walaa! But why can I not define a new class
to separate objects in the same controller?

Regards,

  • Mac

On Jan 15, 3:11Â pm, Michael L. [email protected] wrote:

Frederick C. wrote:

o_O worked without a hitch… why would this be? As you suggested I
threw it back in HomeController (via script/generate controller home
index) under def index and walaa!

Because that’s just how it’s supposed to work

But why can I not define a new class
to separate objects in the same controller?

I’m not entirely sure what you’re trying to achieve. You probably
could create a new controller, but you’d need to set it up so that it
thought it was responding to the same http request etc (ie the work
that rails does when a new request comes in). I’m still not sure what
you’d achieve by that though.

Fred

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs