in my project i have controller in namespace admin and I'm using breadcrumbs_on_rails to build breadcrums. My controller looks like:
module Admin
class FaqsController < Admin::ApplicationController
include FaqsHelper
load_and_authorize_resource
add_breadcrumb t('faqs.faqs_list') , :faqs_path #this line makes the problem
def index
#faqs = #faqs
add_breadcrumb t('faqs.faqs_list')
end
def new
add_breadcrumb t('faqs.new')
end
#other code ommitted
end
end
i can use t method in new, edit and other controller action but when this 't' is not in the controller action i have the follwoing error:
undefined method `t' for Admin::FaqsController:Class
Any ideas?
Use I18n.t instead of just t.
I can suggest to extend your class with
extend ActionView::Helpers::TranslationHelper
It will allow you to use #t and #l helpers.
Thanks Skydan but extend would work only for modules. I made it work by adding include ActionView::Helpers::TranslationHelper to my controller.
Related
my jsonwebtokenauthentication.rb is in (app/lib.jsonwebtokenauthentication.rb):
class JsonWebTokenAuthentication
def some_method
#logic of the method
end
end
I am trying to access the above JsonWebTokenAuthentication method's in my registrations_controller.rb(app/controllers/api/v1/registrations_controller.rb)
class Api::V1::RegistrationsController < Api::V1::BaseController
def create
auth_token = JsonWebTokenAuthentication.some_method({user_id: user.id})
end
end
end
How can we use the class method specified inside lib folder in rails project.
Firstly if you want to use the RoR framework you are supposed to name your files using the Ruby Style guide. Also JsonWebTokenAuthentication looks more like a module, not a class for me, could you please clarify why you choose the class here?
I would suggest adding the some_method to the ApplicationController instead. Another option is to add a json_web_token_authentication.rb to the app/services/ but using the same namespace as the controller.
I have a controller under app/controllers
require_relative '../../lib/bases_helper'
class BasesController < ApplicationController
include BasesHelper
def index
BasesHelper.available_bases
end
end
I am trying to use a method defined in another module under lib:
module BasesHelper
def available_bases
#bases = Base.all
end
end
When I run my application and access the site, I get an error
undefined method `available_bases' for BasesHelper:Module
I can navigate to the method with my IDE by just clicking on its name. Why doesn't it resolve the method? What am I missing?
Although Junan Chakma answer will work, i will advise against setting it up that way. Its better (and follows Rails conventions) to use a private method in your controller and use a callback (i.e. before_action); for example:
class BasesController < ApplicationController
before_action :set_available_bases, only: [:index]
def index
end
private
def set_available_bases
#bases = Base.all
end
end
This will set up #bases instance variable to be used in your index action and index.html.erb view.
I think you don't need to add BasesHelper to use available_bases method. Just use method name like this
def index
available_bases
end
As you imported BasesHelper module in your controller all methods of BasesHelper will be available in your controller. So you can use those method just by calling(without its module name) its name.
If you want to improve your code quality and follow rails conventions then please check Gerry's answer.
It is because your method available_bases is an instance method of BasesHelper, not a class method. And you are calling it as if it were a class method.
If you want to use available_bases like a class method, extend the class instead of include-ing it.
class BasesController < ApplicationController
extend BasesHelper
...
end
I'm relatively new to Rails and am working on creating a simple user authentication system to get to grips with how Rails works.
I'm currently at the point where I'd like to create some methods that I can use in my controllers like so:
is_logged? # => true
and
current_user_id # => 6
These would be used to interact with sessions, mainly so I'm not repeating myself in the controller.
Where would I define these functions and how would I include them in a controller?
Thanks a lot in advance for any help.
Method 1
You can define these method in helper files, inside app/helpers/my_module.rb. You can create a module there, put all the methods inside of it, and then include the modules in your control to use these method.
module MyMoule
def is_logged?
...
end
end
Then in you class include the module
class MyClassController < ApplicationController
include MyModule
def my_method
#Use it like this
logged_in = MyModule.is_logged?
end
end
Method 2
If you using session related stuff you can always put them inside application_controller.rb. And since all your controller will inherit ApplicationController the methods will be available to you.
class ApplicationController < ActionController::Base
def is_logged?
...
end
end
In your other controller you can use them directly.
class MyClassController < ApplicationController
def my_method
logged_in = is_logged?
end
end
I'm trying to create a custom helper like this:
# app/controllers/my_controller.rb
class MyController < ApplicationController
helper :my
def index
puts foo
end
end
# app/helpers/my_helper.rb
module MyHelper
def foo
"Hello"
end
end
But, I got the following error:
undefined local variable or method `foo' for #<MyController:0x20e01d0>
What am I missing ?
Generally, I do the opposite: I use controller methods as helpers.
class MyController < ApplicationController
helper_method :my_helper
private
def my_helper
"text"
end
end
Helpers are accessed from the views, not the controllers. so if you try to put the following inside your index template it should work:
#my/index.html.erb
<%= foo %>
If you do want to access something from the controller, then you should use the include syntax instead of helper, but do not name it like a helper module in that case.
How about just including the helper as a mixin in the controller...
class MyController < ApplicationController
include MyHelper
def index
puts foo
end
end
I created a helper method for some simple calculation. This helper method will just return an integer. I need the helper in both controllers and views.
Unfortunately, it work well in views but not in controllers. I get the undefined local variable or method error. How can I fix it?
Thanks all
In order to use same methods in both controller and views Add you method in application_controller.rb and make it helper methods.
For example
class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
#protect_from_forgery # See ActionController::RequestForgeryProtection for details
helper_method :current_user
def current_user
session[:user]
end
end
Now you can use method current_user in both controllers & views
I use a following solution. Because I think helper's methods shoud be stored in an appropriate helper module.
module TestHelper
def my_helper_method
#something
end
end
class SomeController < ApplicationController
def index
template.my_helper_method
end
end