Can I use helper methods in rake?
相关问题
- Question marks after images and js/css files in ra
- Using :remote => true with hover event
- Eager-loading association count with Arel (Rails 3
- Is there a way to remove IDV Tags from an AIFF fil
- Rails how to handle error and exceptions in model
相关文章
- Right way to deploy Rails + Puma + Postgres app to
- AWS S3 in rails - how to set the s3_signature_vers
- how to call a active record named scope with a str
- How to add a JSON column in MySQL with Rails 5 Mig
- “No explicit conversion of Symbol into String” for
- form_for wrong number of arguments in rails 4
- Rspec controller error expecting <“index”> but
- Factory_girl has_one relation with validates_prese
Yes, you can. You simply need to require the helper file and then include that helper inside your rake file (which actually a helper is a mixin that we can include).
For example, here I have an application_helper file inside app/helpers directory that contains this:
so here is my rake file's content:
and here is the result on my Terminal:
As lfender6445 mentioned, using
include ApplicationHelper
, as in Arie's answer, is going to pollute the top-level scope containing your tasks.Here's an alternative solution that avoids that unsafe side-effect.
First, we should not put our task helpers in
app/helpers
. To quote from "Where Do I Put My Code?" at codefol.io:Since
app/helpers
is intended for view helpers, and Rake tasks are not views, we should put our task helpers somewhere else. I recommendlib/task_helpers
.In
lib/task_helpers/application_helper.rb
:In your
Rakefile
or a.rake
file inlib/tasks
:I'm not sure if the question was originally asking about including view helpers in rake tasks, or just "helper methods" for Rake tasks. But it's not ideal to share a helper file across both views and tasks. Instead, take the helpers you want to use in both views and tasks, and move them to a separate dependency that's included both in a view helper, and in a task helper.