Ruby on rails - Helper method - undefined method `log_in' in Ruby on rails
You are missing the ApplicationController
part here which includes the helper in the controller so its methods can be accessible directly:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
include SessionsHelper
end
Undefined method when calling Helper methods in rails
def self.avatar_path(user, size = 24)
..
end
you are calling instance method add self
before mthod will work for you.
rails “undefined method” when invoking a helper method from the view
Assuming you have Person model. here is an example:
Helper:
def sportists_gender(person)
gender = person.gender
if gender == "1"
"Male"
elsif gender == "2"
"Female"
end
end
Call From View.erb:
<% @person = Person.first %> // any object according to your view.
<%= sportists_gender(@person) %> // display result.
undefined method on custom helper method
Helper methods from controllers aren't available in mailer views. You should put your method to a helper module, for example app/helpers/application_helper.rb
:
module ApplicationHelper
def email_confirmation_path(security_user)
"/security_users/#{security_user.id}/email_confirmation/#{security_user.activation_code}"
end
end
And then you should say to your mailer class to use that helper module (suppose your mailer is app/mailers/mailer.rb
):
class Mailer < ActionMailer::Base
add_template_helper(ApplicationHelper)
end
After that you'll be able to use that helper in your mailer's views.
p.s. also notice that the private
modifier in Ruby doesn't restrict access to a method only to class where it is defined. Subclasses are also able to call it. Private
means that a method can not be called in relation to an object like so obj.my_method
but can only be implicitly called in relation to self
like so my_method
.
undefined method error in helper for a model
You need to extend
instead of include
because the method you want to call it from is a class method.
Also, just a side note, your self.quantity_this_month
loads all the Expense records into memory, but it's possible to do the date filtering and counting all within a single SQL query. The below is how it's done in MySQL, this might be slightly different with other databases:
class Expense < ApplicationRecord
extend Helpers # <~~~~~~~ changed to extend
def self.quantity_this_month
where(
"DATE_FORMAT(date, '%Y/%m') = ?",
year_month(Date.today)
).count
end
end
Undefined method error when calling a helper method in controller from ActionMailer view
You should move your helper method into a separate module and then include the module into both the controller and the mailer using add_template_helper
method. Then the helper methods will be available in controller and mailer views.
module SomeHelper
def some_shared_method
# ...
end
end
class SomeController
add_template_helper SomeHelper
end
class SomeMailer
add_template_helper SomeHelper
end
Note: If you place the code into a helper module (in app/helpers
directory) then you won't need to include the module into the controller as helper modules methods are available in controller views by default. However, you will still have to include the module into the mailer to make the method available in the mailer views.
If you also need to call the helper method in the controller, you can do it using the helpers
method which gives you the access to helper methods.
class SomeController
add_template_helper SomeHelper
def some_method
# ...
# calling a helper method in controller
helpers.some_shared_method
# ...
end
end
Or you can include the module into the controller using include
method for the methods to be accessible in the controller directly.
class SomeController
include SomeHelper
def some_method
# ...
# calling the included method
some_shared_method
# ...
end
end
Rails 5: NoMethodError: undefined method `helper' for MyMailer
if it really is a Mailer as opposed to a Model, you should probably inherit from ApplicationMailer
as opposed to ApplicationRecord
, else it is going to be be looking for tables in your DB to back it.
class MyMailer < ApplicationMailer
.....
end
Related Topics
Ruby on Rails CSV Upload&Import - File Name Too Long
Tilt (Kramdown) Preventing Erb Processing When Rendering Markdown
How to Convert a Large Gem to Standalone Rails App
I Want to Match All Punctuation in My Regexp Except Apostrophes. How to Do That in Ruby
How to Handle Partials in an SCSS Project
How to Collect Real-Time Tweets
Transfer Db from One Heroku App to Another Faster
Ide Sublime2 How to Find Method Definition
Count Overlapping Regex Matches in Perl or Ruby
Hook Before All Delyed Job Success Callback to Save Successfully Completed Jobs
Understanding Ruby Method Parameters Syntax
Make a Ruby Script with Text Io Double Clickable Executable File
Rails App Has Trouble with Inter-Model Saving