Using send_file in helper - ruby-on-rails

I'm trying to use send_file method in application_helper.rb and receiving the following error:
undefined method `send_file' for #<#<Class:0x00000003cc7ad8>:0x00000003cc4ea0>
Is there a way to get around this? I know that I can put my helper inside controller but I don't want to do that.

You can't send a file from a helper.
The send_file method is defined in ActionController::Streaming and is therefore not available in helpers. Helpers are for your code which helps with views. If you're writing a 'helper' for a controller, it shouldn't really be a helper. It should be a module in lib which you require and include in the controller.

You can't use send_file inside your helper.
Explain, why do you need to send file from your view helper and how should it work?

Related

Rails gem: correct place for a controller helper?

I'm working on a private gem for Rails which includes a controller helper.
I doubt between these two places for this helper:
lib/gem_name/my_helper.rb
app/controllers/concerns/gem_name/my_helper.rb
What is the correct place for it?
I think you should use it in controller concern, then include helper module into Application controller
So I will prefer app/controllers/concerns/gem_name/my_helper.rb for creating controller helper inside gem.

Calling an ApplicationController method in a ruby file

How can I call an application controller method inside of a Ruby file? I can't use the traditional MVC architecture.
Thanks!
If the method is meant to be called from both the application controller and some other ruby file, then you need to move that method to another file, probably a Plain Old Ruby Object (PORO). Then, require that file from your controller and from whatever other file needs to use it.
It's a good idea to only have controller-related logic in controllers. Since you're calling this method from something besides a controller, it must not be strictly controller-related logic, so this is the perfect opportunity to move it.
If you have a method in the ApplicationController and you need to call it you can use a trick which is:
ApplicationController.new.#method_here
But you better move the method to a plugin and call it in the ApplicationController for a best practice.
More info here:
How do I call controller/view methods from the console in Rails?
https://stackoverflow.com/a/9159853/2552259
The best approach for your problem is to extract the method in question and put it in a module, like lib/my_utils.rb. Then you can require this file where ever you need it:
# lib/my_utils.rb
module MyUtils
def the_method_you_were_talking_about(args)
.. code ..
end
end
Then:
# application_controller.rb
require 'lib/my_utils.rb'
MyUtils.the_method_you_were_talking_about("and any data you need to pass to it")
And then you'd do the same thing from your other ruby file.

How can I access image_path route helper inside a Rails 3 model?

I'm trying to use Sprockets' image_path method from within a Rails 3 model. I've tried including ActionView::Helpers::AssetTagHelper in my model and calling image_path but that does not work (undefined local variable or method 'config').
Any ideas?
Update - this worked for me:
ActionController::Base.new.view_context.asset_path("image.png")
I think this is the standard way of loading the asset helper:
ActionController::Base.helpers.asset_path("image.png")
This seems preferable to creating a new instance of ActionController::Base and asking for the view context in that it is more explicit.

Rails automatically call application helper methods any time form_for is called

In my application, any time there is a form, I call two helper methods to inject some javascript into the head of the web page. One method injects code to focus on the first input of the form and the second method hooks up some call backs that I use with the client_side_validation gem.
I will be calling these two methods on any page that has a form_for in the view. Is there a way to automatically invoke these methods any time form_for is called as opposed to adding the calls to my template files?
Using Rails 3.1.
You should wrap form_for with your own method that calls your helpers. Fortunately Rails provides an easy way of doing this in the form of alias_method_chain.
module ActionView::Helpers::FormHelper
def form_for_with_my_helpers (*args)
my_helpers
form_for_without_my_helpers (*args)
end
alias_method_chain :form_for :my_helpers
end
You can now call form_for_without_my_helpers with the normal form_for arguments if you don't want a form to use your helpers.
This can go somewhere that's in the load path. Or you can require the file holding this code.
You shouldn't be using Rails helper methods for these. You should be using jQuery. Add this to your application.js file:
$(document).ready(function() {
$('form>input:first-child').focus();
}
That's an example, use a similar method for the callbacks you're talking about.

Using link_to_remote inside of a controller

how can I use link_to_remote inside of a controller?
I have already included ActionView::Helpers::JavaScriptHelper, but still get the error:
undefined method `content_tag'
Thanks!
You'll need to discover all of the helpers that need to be included to make it work. For instance, content_tag is part of ActionView::Helpers::TagHelper.
http://www.railsbrain.com/api/rails-2.2.2/doc/index.html?a=M002139&name=content_tag
It may be time to consider an alternative design.

Resources