I have to change the value of keys inside params of a controller. I don't want to do it each method but do it at a constructor level. But when I access it inside initialize I get this error
"NoMethodError (undefined method `parameters' for nil:NilClass):"
class MyController < ApplicationController
def initialize
puts params.inspect #params is nil!
end
end
I also tried to do that using before_action but the same error
class MyController < ApplicationController
before_action :my_initializer
def initialize
puts params.inspect #params is nil!
end
def my_initializer
puts params.inspect #params is nil!
end
end
Thanks
You should use before_action without initialize to make it work:
class MyController < ApplicationController
before_action :my_initializer
def my_initializer
puts params.inspect
end
end
Related
how can I filter a second before_action?, I have two controllers, and when I call the method A, a before_action executes a methodB, but before executing this second method I would like to execute a general methodC, but in this method I need to pass a parameter to know where is coming from, but if I use a second before_action this doesn't work, because "only" in before_action filters using the first method method (which is methodA), what can I do?
class FirstController < SecondController
before_action :methodB
def methodA
#some code
end
end
class SecondController < ApplicationController
before_action only: [:methodB] do
methodC('methodB')
end
def methodB
#some code new
end
end
class ApplicationController < ActionController::Base
def methodC(method)
#general method
end
end
This is the structure that I have:
I'm out of practice with rails but would the following be acceptable?
class FirstController < SecondController
before_action :methodB
def methodA
#some code
end
private
def should_call_C
action_name == :methodA
end
end
class SecondController < ApplicationController
before_action do |controller|
methodC if controller.should_call_C
end
def methodB
#some code new
end
private
def should_call_C
action_name == :methodB
end
end
class ApplicationController < ActionController::Base
def methodC
#general method
# can use action name to get method rather than passing it?
end
end
In the below, will the implicit render still get called in the SpecialArticlesController? Or will it get skipped to the implicit render in ArticlesController?
class SpecialArticlesController < ArticlesController
def index
...
super
end
end
class ArticlesController
def index
...
end
end
You will not get a double render error, unless you are explicitly rendering twice. This code will cause an error:
class ParentController < ApplicationController
def index
render
end
end
class ChildController < ParentController
def index
render
super
end
end
Whereas the code below will not cause an error:
class ParentController < ApplicationController
def index
end
end
class ChildController < ParentController
def index
super
render
end
end
I have a helper, controller and template like:
Helper:
# app/helpers/application_helper.rb
module ApplicationHelper
def current_user
#current_user ||= User.find_by(access_token: access_token)
end
private
def access_token
pattern = /^Bearer /
header = request.headers["Authorization"]
header.gsub(pattern, "") if header && header.match(pattern)
end
end
Controller:
# app/controllers/api/v1/companies_controller.rb
class Api::V1::CompaniesController < Api::V1::BaseController
before_action :set_company, only: [:show]
def show
render #company
end
private
def set_company
#company ||= Company.find(params[:id])
end
end
# app/controllers/api/v1/base_controller.rb
class Api::V1::BaseController < ApplicationController
respond_to :json
end
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
include ApplicationHelper
protect_from_forgery with: :null_session
end
RABL-Rails template:
object :#company
attributes :id, :name, :description, :website
# --- How can I call a helper method here?
# if (#company.owner?(current_user) or current_user.kind_of?(Admin))
# attributes :contact
# end
attributes :created_at, :updated_at
When I call a helper method from RABL template, it will raise an error:
undefined local variable or method `current_user' for #<RablRails::Compiler:0x00000002494c68>
How can I call a helper method from RABL template?
Note: I used gem rabl-rails '~> 0.4.1'.
It seems your calling it the right way but the real problem is that your controller doesn't have inheritance from ApplicationController (unless there is more in Api::V1::BaseController that we can't see). So this means that your probably not getting ApplicationHelper included.
I would suggest you just add it to your controller
class Api::V1::CompaniesController < Api::V1::BaseController
include ApplicationHelper
...
end
How to invoke method once per page reload? defining before_filter in ApplicationController is failed because of multiple controllers used to perform action
When you say multiple controllers are used in one action, could you be more specific? Normally a before_filter in the controller responsible for the action would suffice.
If you want your before filter to affect specific methods in multiple controllers then place the method itself in the ApplicationController but the before_filter in each of the controllers that contain the action.
class ApplicationController
def foo
#bar = 'bar'
end
end
class UserController
before_filter :foo, :only => [:method1]
def method1
...
end
def method2
...
end
end
class StuffController
before_filter :foo, :only => [:method2]
def method1
...
end
def method2
...
end
end
class UnimportantController
# No before filter, neither of these methods will call :foo
def method1
...
end
def method2
...
end
end
I have a controller and every method of it starts with the following code:
#user = UserData.find_by_login(session[:cuser])
if #user == nil
redirect_to(:controller=> 'user_data', :action=> 'login')
return
end
I'm just wondering if it is possible to avoid code duplication in this case ?
Yes, use a before_filter
class YourController < ApplicationController
before_filter :check_user
def check_user
..
end
end
Absolutely.
class MyController < ApplicationController
before_filter :ensure_logged_in
# actions here.
def ensure_logged_in
#user = UserData.find_by_login(session[:cuser])
if #user == nil
redirect_to(:controller=> 'user_data', :action=> 'login')
end
end
end
You shouldn't need to worry about the 'return', as rails will bail out of the filter pipeline once the redirect happens.
To avoid duplication you just need to add before_filter in every controller where you want to check user authentication.
class SomeController < ApplicationController
before_filter :authenticate_user
end
then add your user authentication logic in application controller something like this,
class ApplicationController < ActionController::Base
private
def current_user
#current_user ||= UserData.find_by_login(session[:cuser]) if session[:cuser]
end
helper_method :current_user
def authenticate_user
redirect_to({:controller=> 'user_data', :action=> 'login'}, :alert => "Not authorized") if current_user.nil?
end
end
You can use current_user helper method in every controller to get current user.
Try to use before filter. This should be fine