How can I call send_data and send_file from other classes (outside of controller)

rails before_action
rails after action
rails send_data
rails action controller
rails before_action except
rails controller callbacks
rails params
rails strong params

How can I call send_data and send_file from other classes (outside of controller)?

For example in my index controller, I have send_file and/or send_data. I would like to call them from my own special class.

I have some codes in this class and would like to continue in same method without exposing anything.

Sample:

app/controller/index_controller.rb:

...
def index
   t = TestClass.new
   t.do_something
end
...

app/package/test_class.rb

class TestClass
   def do_something
      send_file params... # GIVES ERROR BECAUSE THIS IS NOT A RAILS CONTROLLER.
   end
end

Is there any way to call send_file/send_data from this class? Any way to pass this method? If possible, how? If possible but not recommended, again why?

It is possible, but not recommended. Why? Because send_data and send_file are the reasons why controllers even exist! It is [part of] their purpose: parsing input and generating response.

But what if I insist?

The simplest thing would be to inject reference to controller into your class. Something like this should work.

def index
  t = TestClass.new(self)
  t.do_something
end

class TestClass
  attr_reader :controller

  def initialize(controller)
    @controller = controller
  end

  def do_something
    controller.send_file(...)
  end
end

Advanced Rails, The easy way to do this is with the send_file or send_data API calls, which stream data from the server to the client: class DataController However, the files should be outside of the web root, lest someone guess a filename and have server's path to the file to send, in conjunction with the other standard HTTP headers. How I can call function defined under controller from any place of web page (outside of controller component)? It works perfectly when I press "get" button. But I need to call it from outside of div controller. The logic is: by default my div is hidden.

While most things are possible in Ruby, you really shouldn't be calling send_file outside of the controller because it undermines the MVC structure of rails. If yous start down this path, you'll confuse anyone who looks at your code, including your future self, and your project could become increasingly un-maintainable.

If you'd like to have a helper method which supplies the data you'd like to send, then does some cleanup, you can achieve it using a block:

class TestClass
  def do_something
    #set up params
    yield params
    #clean up or do other work
  end
end

class IndexController < ApplicationController
  def index
    t = TestClass.new
    t.do_something |params|
      send_file params
    end
  end
end

send_data (ActionController::DataStreaming), You may also set the content type, the file name, and other things. Options: :​filename - suggests a filename for the browser to use. :type - specifies an HTTP  I've been working on this for two days. I need to pass an arraylist to another class. It gets the information, and will print out the menu, until the program goes back to the initial class, then the list empties. I don't get it, and I'm so confused. public class Party { private String inputLine

I would like to respectfully disagree with the 'don't do it' answers.

The Presenter Pattern is all about rendering outside the controller. Sergio Tulentsev's answer (which is consistent with the Presenter Pattern) works well when you're operating in the context of a single request cycle.

Sometimes, you need to operate outside a single request cycle (as mentioned here by DHH himself). In such cases, you can use the approach discussed in the linked thread.

I use both frequently.

send_file (ActionController::Streaming), send_file. Importance_5. v2.2.1 - Show latest stable - 4 notes - Class: Read about the other Content-* HTTP headers if you'd like to provide the user with Nginx supports a similar http header to X-Sendfile called X-Accel-Redirect. I inherited some code that used form_remote_tag. send_file and send_data did not work. Its has five functions besides constructor and destructor, called Add, Subtract, Devide (that was a spelling mistake), Multiply, and DisplayOutVal. As you can see from their names, these functions add, subtract, multiply, and divide and store the results in a private variable iOutVal which is called by DisplayOutVal to display the results.

I like the approach @eiko lays out as it rightly keeps send_data in the controller but lets complexity live elsewhere. For others wanting to follow this approach but unsure of the specific block implementation (I wasn't, at first), here's what I ended up with. Within the controller:

respond_to do |format|
  format.pdf do
    exporter = EventRequirementExporter.new(@event_requirement)
    exporter.export_pdf do |data, filename, mime_type|
      send_data data, filename: filename, type: mime_type, disposition: 'attachment'
    end
  end
end

And in the EventRequirementExporter:

def export_pdf
  pdf_combiner = CombinePDF.new
  # (buncha stuff happens)
  yield pdf_combiner.to_pdf, combined_filename(:pdf), 'application/pdf'
end

The operative part is the yield, which populates the relevant variables in the order supplied in the block. HTH someone.

Action Controller Overview, For more details on the routing process, see Rails Routing from the Outside In. A controller is a Ruby class which inherits from ApplicationController and has The first are parameters that are sent as part of the URL, called query string All controllers in Rails have the send_data and the send_file methods, which will  4. you can implement interface in the mainactivity and pass this to class. You can call interface.doSomeWork.This is the best way to go in most situations. 5. Create Mainactivty as singleton and call MainActivity.getInstance().doSomeWork().This works well for controller classes . 6. Create broadcast receiver ,register with activity and call it from the class you will need context though. 7. You use handler and pass messages . 8.

Action Controller Overview, A controller is a Ruby class which inherits from ApplicationController and has methods The first are parameters that are sent as part of the URL, called query string Otherwise, the key is going to be filtered out, so arrays, hashes, or any other All controllers in Rails have the send_data and the send_file methods, which  The method is private. You can not access private members of other classes. Also some other problems here: The method you wrote is an instance method, so you need to have an instance of the class to call the method. If you want to call it using UploadFiles.EnsureDirectoryExists(), you need to make it a class method (static).

Send_file and remote files - Rails, I'm making a Flash application that loads external files from other servers onto the user's class StreamDataController < ApplicationController How to call private method from another class in java You can call the private method from outside the class by changing the runtime behaviour of the class. By the help of java.lang.Class class and java.lang.reflect.Method class, we can call private method from any other class.

scrapbook2/2014-03-render-images-with-rails-controller.md at , class BrandingController < ApplicationController def logo send_file 'public/rails.​png', type: What I found out interesting was that when I didn't specify content type in send_data & send_file newer browsers were Another way is to determine mime type from file name Contact GitHub · Pricing · API · Training · Blog · About. At times you need to pass data from an action method belonging to one controller to an action method belonging to another controller. There are three ways to accomplish this task. They are - Pass data as query string, pass data in TempData, and pass data as route parameters. Let's quickly see how each of these three approaches work.

Comments
  • Due to both answers, I will not call it from outside of a controller. So I will expose my objects (only necessary ones) and use them in controller.
  • Both answers are OK for me. But this has what I want and plus, answered earlier then other. So I accept this.
  • Due to both answers, I will not call it from outside of a controller. So I will expose my objects (only necessary ones) and use them in controller.
  • Yep, presenters are good. It is one thing, though, to do this because you have presenters and totally different when you throw rendering code in the same pile with some business logic "just because". If it's the latter, then "don't do it" :)
  • Also, what kind of presenter pattern did you mean? The one described here doesn't do any rendering. That kind of presenter simply extracts presentation-related logic from models, which otherwise would've been more bloated (date/currency formatting and such)
  • I agree, @SergioTulentsev. It's almost never good to do something "just because". But, if there's good reason (as you say), then I say "go for it". There's nothing inherently wrong with passing the controller around or even instantiating a new one as needed.
  • Ryan Bates has a nice RailsCast on Presenters that do rendering: youtube.com/watch?v=QHsq6aFlP0U
  • Or maybe I'm misunderstanding something. Anyway, presenter is a useful pattern. :)