NoMethodError in Admin::DashboardController#index - ruby-on-rails

am new to rails, and am using active admin for a work, and i get this error whenever i open active admin dashboard
NoMethodError in Admin::DashboardController#index
undefined method `asideSection' for #<Admin::DashboardController:0x00007fc544017d70>
here is my application_conrtoller.rb
class ApplicationController < ActionController::Base
before_action :asideSection
def hhome
end
def getAsideSection
#asideSections = Page.all
end
end
how can i fix it please.

before_action :asideSection tries to call a method named asideSection.
This method does not exist.
However, you have defined a method named: getAsideSection. I presume that this is what you want to be called.
So, you could either change that to: before_action :getAsideSection, or rename the method to asideSection.
Here is how I would write it, also following the ruby style guide convention of using snake_case for variables and method names:
class ApplicationController < ActionController::Base
before_action :get_aside_sections
def home
# ...
end
private
def get_aside_sections
#aside_sections = Page.all
end
end

Related

Undefined method 'permissions_for' for Permissions:Module in Rails 6

I am returning to Rails after 5 years out of it and trying to understand the changes. I am going through Ryan Bates' Railscasts looking to update a template I built some years ago and am getting the above error when initializing the permissions class for authentication. (See RC#386 about 18:00 into the playback.)
Rails has changed the before_filter to before_action (makes sense...) and I have the following in the ApplicationController:
class ApplicationController < ActionController::Base
before_action :authorize
delegate :allow?, to: :current_permission
helper_method :allow?
delegate :allow_param?, to: :current_permission
helper_method :allow_param?
private
def current_user
#current_user ||= User.find(session[:user_id]) if session[:user_id]
end
helper_method :current_user
def current_permission
#current_permission ||= Permissions.permission_for(current_user)
end
def current_resource
nil
end
def authorize
if current_permission.allow?(params[:controller], params[:action], current_resource)
current_permission.permit_params! params
else
redirect_to root_url, alert: "Not authorized."
end
end
end
My permissions.rb file has the following:
module Permissions
def self.permission_for(user)
if user.nil?
GuestPermission.new
elsif user.admin?
AdminPermission.new(user)
else
MemberPermission.new(user)
end
end
end
I'm getting the above error: NoMethodError at /undefined method "permission_for" for Permissions:Module from BetterErrors (and Puma). However the method should be defined in the Permissions module; it's right there. Yet somehow, something has changed in Rails that I can't figure out.
I have tried to require the file: nothing.
Any help?
You need to include a module in a class to use it. It then adds functionality to that class, you do not access methods in module itself.
I'm guessing you need to do something like this (not tested):
class ApplicationController < ActionController::Base
include Permissions
before_action :authorize
change your module to:
module Permissions
def permission_for(user)
...
and then permissions_for is available in your controller
def current_permission
#current_permission ||= permission_for(current_user)
end
The only thing that has changed in Rails is how folders and files are included. The lib folder inside your rails app is not autoloaded by default.
I think best practice is now to add a lib folder within the app folder and then put all your modules and other classes in there. They will be included as soon as you restart your server.

Method not required for GET? If so, is it common not to write it?

I'm new to Rails, and I was thinking that a method should be defined for every single route.
However, /hello_world works as long as I write as below:
Rails.application.routes.draw do
get "/hello_world", to: "hello#world"
end
class HelloController < ApplicationController
# no world
end
# app/views/hello/world.html.erb
hello world!
Is it an expected behavior? If so, is it common not to write it?
You only need a controller action if you have any processing / data retrieval that you need to do before you display the view.
So, yes, the method itself isn't necessarily needed.
You might need to retrieve a record in a show action like this...
class CustomersController < ApplicationController
def show
#customer = Customer.find(params[:id])
end
end
but inn some cases, you might have a before_action that does whatever's neeeded for several methods, so you (again) don't need to specify the action method.
class CustomersController < ApplicationController
before_action :set_customer only: [:show, :edit, :update]
private
def set_customer
#customer = Customer.find(params[:id])
end
end
So this is a case where you might have needed to define a method for the action, but you've now made it unneccessary.

Trying to Create Basic API with before_action method

First time trying to create an API and am having some issues. I want to use the Host header in the post request to find the property that the lead is supposed to be routed to. However I get undefined local variable or method for my method that is supposed to pull the header out and find the correct property. Here is the controller:
class LeadsController < ApplicationController
before_action capture_header, only: :create
skip_before_action :verify_authenticity_token
def create
#property.leads.create!(lead_params)
end
private
def lead_params
params.permit(:firstname, :lastname, :email, :phone, :moveindate, :source)
end
def capture_header
referrer = request.headers["HTTP_REFERER"]
domain = URI.parse(referrer).host.match(/\w*.com$/)[0]
#property = Property.where(url: domain)
end
end
The error:
undefined local variable or method `capture_header' for
LeadsController:Class
Any insight would be greatly appreciated.
missing symbol for method "capture_header".
Please try: before_action :capture_header, only: :create

How to get the name of the action in an after_action filter for ActionMailer

In the class below, how do I get the current action name (i.e. email_confirmation, password_reset) in side the after_action callback add_mandril_headers?
class UserMailer < ActionMailer::Base
after_action :add_mandril_headers
def email_confirmation(user)
mail(..)
end
def password_reset(user)
mail(..)
end
private
# how to get the action name?
def add_mandrill_headers
headers['X-MC-Tags'] = [mailer_name, action_name].join('_');
end
end
Turns out action_name returns the current mailer action name. I tried it based on the fact that ActionController has a similar method.
Thanks #HarishShetty!
As you mentioned, the action_name is good for all Controllers, as it is inherited from ApplicationController.
For example, I was using public_activity and wanted some simplification in my controllers:
class SiteDetailsController < ApplicationController
after_action :track_activity, only: [:create, :update, :destroy]
# ...
private
def track_activity
#site_detail.create_activity action_name, owner: current_user
end
end

access application_helper in before_action

I have the following in my application_helper.rb file:
module ApplicationHelper
def require_employer_profile_for_employers(page)
if current_user.type == 'Employer'
if current_user.employer_profile
else
flash[:error] = "You must create a profile before accessing #{page}."
redirect_to new_employer_profile_path
end
end
end
end
I try calling it in my projects controller like this:
before_action "require_employer_profile_for_employers('Projects')"
but my server responds with this error:
NoMethodError (undefined method `require_employer_profile_for_employers' for #<ProjectsController:0x007fb741f82e38>):
How do I access the helper in the before_action in the projects controller?
include ApplicationHelper in your ProjectsController:
class ProjectsController < ApplicationController
include ApplicationHelper
# ...
end
Helpers are not directly accessible within a controller as opposed to view layer where they are freely accessible.

Resources