i have a problem:
undefined method `avatar_url' for #<User:0x000000040783a8>
controller:
class ApplicationController < ActionController::Base
protect_from_forgery
helper :all
helper_method :avatar_url
protected
def avatar_url(user)
if user.avatar_url.present?
user.avatar_url
else
default_url = "#{root_url}images/guest.png"
gravatar_id = Digest::MD5.hexdigest(user.email.downcase)
"http://gravatar.com/avatar/#{gravatar_id}.png?s=48&d=#{CGI.escape(default_url)}"
end
end
end
in view:
...
<%for user in #users %>
<%= image_tag avatar_url(user) %>, username: <%= user.username %>, e-mail: <%= user.email %>
<% end %>
...
Someone help me?
The key is the error message:
undefined method `avatar_url' for #<User:0x000000040783a8>
The error isn't because the helper method is undefined: the error message indicates it's a missing method in the User class. The method defined in ApplicationController attempts:
def avatar_url(user)
if user.avatar_url.present?
user.avatar_url
#...
You need to make sure the User class has an avatar_url instance method.
Better you can remove the line "helper_method :avatar_url" from the file ApplicationController.
Please see more details here. "undefined method" when calling helper method from controller in Rails
Because, you should define the helper method xxx (helper_method :xxx) in any other module/helper file. So, the line helper :all automatically include all your helper files then only you can use the line helper_method :xxx. It makes sense.
No need to define the ApplicationController methods as a helper method. You can call those methods in any controller or views in simply avatar_url().
Related
I can use Devise helper methods in regular views but don't know how to use it within my Mailer. I need to determine if user is signed in to construct proper email message.
class UserMailer < ActionMailer::Base
def receipt
end
end
receipt.text.erb
<% if user_signed_in? %> #Error: undefined method `user_signed_in?' for #<#<Class:0x35695fc>
Secret link
<% end %>
Actually, you can't and most of all, you shouldn't use this kind of Devise helper on your mailer.
Why ? Well, if you search Devise's code base for the user_signed_in? helper, you will find it in Devise::Controllers::Helpers module, as you can see here. This means that it is supposed to be used in a controller context, as it uses method such as request and warden that is only available on controllers.
If you must make any decision in your mail view based on whether a user is signed in or not, I would recommend you to pass this information from your controller to your mailer:
Your controller:
class UserController < ApplicationController
def your_action
UserMailer.receipt(user_signed_in?).deliver
#....
end
end
Your mailer:
class UserMailer < ActionMailer::Base
def receipt(signed_in)
#signed_in = singed_in
#....
end
end
Your mailer view:
<% if #signed_in %>
Secret link
<% end %>
I hope it helps !
You can pass it over from helper. Something like this
class UserMailer < ActionMailer::Base
def receipt
#is_signed = user_signed_in?
end
end
and
<% if #is_signed %>
Secret link
<% end %>
I am having a action in application controller
def is_customer_logged_in?
!!session[:customer_id]
end
And in my view am trying to access the application_controller action like this
<% unless is_customer_logged_in? %>
some functions
<% end %>
The above code is a partial layouts.
This is the error message I am facing
undefined method `is_customer_logged_in?' for #<#<Class:0xb51a5300>:0xb5616484>
You can define it to be a helper method and you should be able to access that method in the view.
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
def is_customer_logged_in?
!!session[:customer_id]
end
helper_method :is_customer_logged_in?
end
try helper_method: is_customer_logged_in?
orry for the basics, but I'm having a hell of a time getting a very basic flow to work:
1) Define module with a method to save a url to a var (or return it)
2) Call that method in a controller to initialize the method
3) Have a view show that URL
NoMethodError in AuthController#oauth undefined method `oauthurl' for GetAccessToken:Module
Module: \lib\get_access_token.rb
module GetAccessToken
CONSUMER_TOKEN = { :token=>"mytokenstringwhichisreallylong", :secret=> "mysecretstringwhichisreallylong" }
def self.oauthurl
#oauthurl="https://us.etrade.com/e/t/etws/authorize?key=#{(CONSUMER_TOKEN[:token])}&token="
end
end
Controller: app\controllers\auth_controllers.rb
require 'get_access_token'
class AuthController < ApplicationController
include GetAccessToken
before_filter :oauthurl1
def oauthurl1
GetAccessToken.oauthurl
end
end
View: app\views\auth\oauth.html.erb
<% provide(:title, 'oAuth') %>
<h1>oAuth</h1>
<%= link_to "oAuth", #oauthurl %>
My higher level goal is to get the eTrade oAuth flow working, but I want to make sure I understand every line of code vs. taking someone else's and I can't get this very basic building block to work yet.
Add the following to config/application.rb:
config.autoload_paths += Dir["#{config.root}/lib/**"]
Change your AuthController to:
class AuthController < ApplicationController
include GetAccessToken
def oauthurl1
GetAccessToken.oauthurl
end
end
Your module code will be
module GetAccessToken
CONSUMER_TOKEN = { :token=>"mytokenstringwhichisreallylong", :secret=> "mysecretstringwhichisreallylong" }
def self.oauthurl
"https://us.etrade.com/e/t/etws/authorize?key=#{(CONSUMER_TOKEN[:token])}&token="
end
end
and your controller code should be
require 'get_access_token'
class AuthController < ApplicationController
include GetAccessToken
def oauthurl1
#oauthurl = GetAccessToken.oauthurl
end
end
We need to initialize #oauthurl in the controller to use this variable in the view, else it will be nil.
With the generous help of the contributions above, this is how I finally resolved the error. I defined the instance variable in the controller instead of the model, and initialized the controller method using before_filer:
Model:\lib\test_module.rb
module TestModule
CONSUMER_TOKEN = { :token=>"myReallyLongToken", :secret=> "myReallyLongSecret" }
def self.testUrl
"https://us.etrade.com/e/t/etws/authorize?key=#{(CONSUMER_TOKEN[:token])}&token="
end
end
Controller: app\controllers\test_controller.rb
require 'test_module'
class TestController < ApplicationController
include TestModule
before_filter :testUrl1Init
def testUrl1Init
#testurl=TestModule.testUrl
end
end
View: \app\views\test\test.html.erb
<% provide(:title, 'test') %>
<h1>test</h1>
<%= link_to "test link", #testurl %>
I defined a helper class as below
module SessionsHelper
def current_user
#current_user= User.find_by_fbid(session[:fbid])
end
def sign_in(user)
session[:fbid] = user.fbid
#current_user = user
end
def signed_in?
!current_user.nil?
end
end
I included the Helper Class in my Application Controller
class ApplicationController < ActionController::Base
protect_from_forgery
include SessionsHelper
end
The sign in method gets called from Session Controller
class SessionsController < ApplicationController
def create
user = User.find_or_create_by_fbid(params[:user][:fbid])
user.update_attributes(params[:user])
sign_in(user)
redirect_to user_path(user)
end
end
However I am not able to access 'current_user' variable from users#show view.
<% if signed_in? %>
<p>
<b>Current User:</b>
<%= current_user.name %>
</p>
<% end %>
It says : undefined method `name' for nil:NilClass
Can anyone please advise ?
The method current_user does not get called at all from index.
Putting include SessionsHelper in your controller includes those module methods in the controller, so they are accessible in your controller methods. You want the helper methods available in your views, so you need to use helper SessionsHelper in your application controller.
That being said, I do agree with Jits that the methods you have in SessionsHelper really do belong in the controller instead of in a helper.
Generally you should have methods like current_user defined in your application_controller and then make them available as helpers in the views. This way the controllers have access to them (and trust me, you will most likely need access to things like that). Example:
def current_user
..
end
helper :current_user
What helped me:
Define methods to use in the controller in helper files
Define methods to use in the view in the relevant model file
Example
Suppose you had this in user_helper.rb
def something
2 + 2
end
simply move that code into
models/user.rb
and it will be accessible in the view without any further effort.
Noob scoping issue, I imagine. :\
class ApplicationController < ActionController::Base
protect_from_forgery
#locations = get_locations
def get_locations
Location.where(:active => true).order('name').all
end
end
Error:
undefined local variable or method `get_locations' for ApplicationController:Class
Two questions:
1) What's with the error? Am I calling the method incorrectly?
2) How do I access this method from a sub-classed controller?
You're calling get_locations within the class scope, but the method is an instance method, not a class method. If for example you used def self.get_locations then you would be providing a class method, one of which you can use within the class scope (after you have defined it, not before like you're doing).
The problem here is the logic, what is this method for? What do you intend to use #locations for? If it's to go inside your application view, then you should put this method into the ApplicationHelper module, and call it from inside the relevant action. If you'd like it in another view on another controller and you'd like to use #locations inside your locations method, perhaps your setup might look something like this:
PagesController
class PagesController < ActionController::Base
def locations
#locations = Location.where(:active => true).order('name').all
end
end
locations.html.erb
<% #locations.each do |location| %>
<%= # do something with 'location' %>
<% end %>
If you'd like to use this inside of your application.html.erb you can simplify it quite some..
ApplicationController
class ApplicationController < ActionController::Base
protect_from_forgery
def locations
Location.where(:active => true).order('name').all
end
end
application.html.erb
<% locations.each do |location| %>
<%= # do something with location %>
<% end %>
The answer boils down to logic, and to really figure out exactly what you're looking for, more details would probably be required.
You're calling it from the class scope, not from an instance scope. more likely what you want is the following:
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :setup_locations
private
def setup_locations
#locations = Location.where(:active => true).order('name').all
end
end
To make your original example work, you'd need to make #get_locations defined on self (which points to the class at definition), like so:
class ApplicationController < ActionController::Base
protect_from_forgery
#locations = get_locations
def self.get_locations
Location.where(:active => true).order('name').all
end
end
The problem with that code is that #locations will only be available from the class level as a class instance variable, which is comparable to a static variable in most other languages, and which probably isn't what you want.
I imagine that this line:
#locations = get_locations
... is trying to access the class level method get_locations and not the instance method.
The clue here is that the error message is showing that it can't find it on the class itself (ApplicationController:Class) and not an instance of that class. That means that you're in the class scope, not instance scope.
This would fix it:
def self.get_locations
Location.where(:active => true).order('name').all
end
Even the question is quite old, you can also call your controller action anywhere just by calling:
ApplicationController.new.get_locations