What is this access line of code in my Rails controller? - ruby-on-rails

I have the following in my controller but I don't know what the access line means. I'm new to Rails 4. Is it for some gem like devise or something? I've looked through Google and don't see any documentation for the second line here. Any help would be great!
class MyCoolController < ApplicationController
access user: :all, all: [:my_action_name]

You can ask Ruby where the method was defined by booting your application / loading that controller with some debugging inserted:
class MyCoolController < ApplicationController
p method(:access).source_location

Related

How to find where is 'uninitialized constant' called in Rails?

I'm on Rails 6 and using devise and cancancan (and lots of other gems too).
When I call User.find(id).destroy! in rails console, it rollbacks with
NameError Exception: uninitialized constant User::Interactive
Also tried with byebug with the same result.
My head is spinning when I think about looking through all the files for the place where it is called.
I'd appreciate a clue how to track an uninitialized constant with less costs.
UPDATE
Already done by looking through gems.
UPDATE 2 - controller
class UsersController < ApplicationController
load_and_authorize_resource
#REST actions
end
The issue is that you are trying to call this in IRB, which doesn't load your application.
Try running it in the rails console instead ($ rails c)
so you should have controller like this.
class User::InteractiveController < ApplicationController
end

NoMethodError (undefined method `encode' for JsonWebToken:Module):

I am referring to this blog for token based authentication. I am trying to access the jsonwebtoken.rb methods as specified in the above blog in my app/controllers/api/v1/registrations_controller.rb.
class Api::V1::RegistrationsController < Api::V1::BaseController
def create
auth_token = JsonWebToken.encode({user_id: user.id})
end
end
end
My jsonwebtoken.rb file is inside app/lib folder as specified in the blog mentioned above. I have no idea how to use the methods of jsonwebtoken.rb in my different controllers.
Also I have added config.autoload_paths << Rails.root.join('lib') to autoload the file in config/application.rb. Please help me. Somewhere, I am missing the concept.
I think the issue is in the file naming convention see your file name is jsonwebtoken.rb but the class is JsonWebToken so the file name will json_web_token.rb look at the console
2.3.4 :019 > "JsonWebToken".underscore
=> "json_web_token"
Rails UnderScore method.
Restart the server after made any changes.
Here is the very nice tutorial for RESTful JSON API With Rails 5, you can check this.

Rails 4 Superclass mismatch in production only with admin namespace

So I wrote a little portfolio app in Rails 4. It has an admin namespace to allow me to only expose certain controllers to public.
Everything works great when in development, but when I deploy to a production env (or switch to production on my dev machine) I get the following error:
superclass mismatch for class WorksController (TypeError)
My controller structure looks like this:
controllers ->
admin ->
admin_controller.rb
portfolio ->
works_controller.rb
portfolio->
works_controller.rb
As you can see, I namespaced works inside of portfolio.
The controllers are declared as following:
/controllers/admin/admin_controller
class Admin::AdminController < ApplicationController
/controllers/admin/portfolio/works_controller
class Admin::Portfolio::WorksController < Admin::AdminController
/controllers/portfolio/works_controller.rb
class Portfolio::WorksController < ApplicationController
Now for my routes.rb, note I am using Friendly ID so I used a custom route to have a pretty client facing url.
get '/portfolio/:id' => 'portfolio/works#show', as: 'portfolio_work'
namespace :admin do
namespace :portfolio do
resources :works
end
end
So I know I must have screwed this up somehow, but I don't know how. The error I get is related to redeclaring a class, but I don't know how I managed to do so.
Any help is greatly appreciated.
Found this fix after #ReggieB tried to help me. Turns out having my Devise model 'Admin' and a namespace also called 'Admin' creates problems (as it should).
What happens if you redefine /controllers/admin/portfolio/works_controller line this:
module Admin
class Portfolio::WorksController < AdminController

how to skip before_action from alchemy-devise user controller and user session controller? Rails

I have a project that is using alchemy cms and alchemy-devise gem. The gem has a method executed in the controllers of the gem as a before_action
How can I skip this method from being executed directly from the gem?
https://github.com/magiclabs/alchemy-devise/blob/master/app/controllers/alchemy/users_controller.rb
line 6 and block on line 40
this code comes directly from the gem, how can i stop it from been executed?
You want to prevent the user from being redirected to the dashboard, right?
I don't know why you want to do that, but to answer the question you could do following (untested):
# config/application.rb
module YourApp
class Application < Rails::Application
# ...
config.to_prepare do
Alchemy::UsersController.skip_before_action :check_user_count
end
end
end
For my interest: Why do you want that?

Rails: request.subdomains raises undef method for request when in application controller

I am trying to extract the subdomain in the application controller, for app wide use, this way,
#subdomain = request.subdomains(0)
While this code works in any other controller, in the app controller it throws an
undefined local variable or method `request' for ApplicationController:Class
exception.
I am running rails 3.2.2 on Lion.
I borrowed it from DHH's code fragment for basecamp style subdomains -
class ApplicationController < ActionController::Base
  before_filter :set_current_account
  private
    def set_current_account
      #current_account = Account.find_by_subdomain!(request.subdomains.first)
    end
end
What am I missing?
When you're getting that error, it's happenning becaue you're calling request inside ApplicationController's class instead of inside a method. Are you sure that the code you've shown us is correct?

Resources