undefined method 'authorize' with Pundit - ruby-on-rails

I've installed the gem Pundit in my Rails app, and have followed the instructions in the README carefully.
However, when I use authorize in any of my controllers, I get the error message "undefined method 'authorize' for .
Also, when I try to use "policy" in a view, I get the error "undefined method 'policy'".
It's as if Pundit weren't installed at all!
I've included Pundit in my Application Controller.
I have an ApplicationPolicy file with all of my policies, then policy files for each type of record that inherit from the Application Policy.
For some reason, I can't paste any of my code into this question, so I'm hoping I've included enough information!
I'd appreciate any thoughts.

I have recently used pundit and it worked like a charm.
your error messages makes me infer that you've not run install
rails g pundit:install or your policies file are not in correct directories.
or if they are then Just restarting the server should fix this. Hopefully.

For me, I was missing include Pundit at the top of my ApplicationController. Adding that line fixed it.

You need to generate a default policy file for your application with the pundit:install generator.
rails g pundit:install
create app/policies/application_policy.rb

Related

devise ActiveRecord::StatementInvalid in Devise::SessionsController#new error

I'm a new to rails so I'm still learning some concepts.
I installed the ruby gem devise to help with adding users/database etc..
I'm getting an error when I'm trying to access paths provided by devise.
The URL for my page is
https://rubypractice-minhaja.c9users.io
And for example if I try to access
https://rubypractice-minhaja.c9users.io//users/sign_in
I get the same error mentioned in my title I've tried for a long time to look up for a solution but I'm yet to find one.
I did check rails routes and all the routes are there so I'm not sure what the issue is.
https://github.com/minhajahmed1/event_platform
Above is the link to my github if that helps. I would really appreciate any help I get, thanks.
The line
$ rails generate devise MODEL
Was just an example, if you read the instructions you should have replaced "MODEL" with your model name "User"
$ rails generate devise User

problems with sign out, cookies and devise on rails

I have been trying to sign in after signing out on my application using devise but I am having an issue with the cookies I think because I have to refresh my web page in order to sign in again. I tried using activerecord-session_store gem and I follow the steps to configure it. I mean, I installed the gem, I generated the recommended migration on the gem documentation and generated the table using rake db:migrate command, I also changes the session_store.rb file but I feel that I am missing something to make it work since I tried it and the same happens. Please help me if you feel I am missing something or if you have other solution for this problem. Many thanks in advance
You are probably getting a "Can't verify CSRF token authenticity" error. Can you check your server logs?
In your ApplicationController please add the following and check if you get an error.
protect_from_forgery with: :exception

Where can I look at the Rails controller high-level superclasses?

I am trying to understand some of the higher level functioning of Rails, using the Rails console. I run controller.class.superclass.superclass which gives ActionController::Base, controller.class.superclass.superclass.superclass which gives ActionController::Metal and controller.class.superclass.superclass.superclass.superclass gives AbstractController::Base.
I have found these in the API documentation.
http://api.rubyonrails.org/classes/AbstractController/Base.html
http://api.rubyonrails.org/classes/ActionController/Metal.html
[can only post two links]
I can add to these simply by declaring the classes again in the console, but is there a way to find the original Ruby code for these and to inspect and edit it in its original file(s)? Just in case I need to know the full contents of these for future.
You can also do:
bundle show <gem>
and that will show you where the gem is on your system. Editing in those files is not advised unless you know how to re-install gems.
You can see the Rails source code on Github:
https://github.com/rails/rails/tree/master/actionpack/lib/action_controller

Adding api to an existing rails project with devise

I need to add some api for moobile to my existing project in rails. I am using devise gem for authentication. The first api needed are user registration, login, profile update , some posting feature etc.I am following https://github.com/lynndylanhurley/devise_token_auth this to create api, but it creates user.rb and migration as well as a duplicate routes. Am I doing something wrong. Please help me to solve the issue . Thanks in advance
I have added devise token authentication for api. Also created a seperate application controller for api's. All the api controller extends this application controller. The api routes starts with /api/
Documentation says:
A model will be created in the app/models directory. If the model already exists, a concern will be included at the top of the file.
And
A migration file will be created in the db/migrate directory. Inspect the migrations file, add additional columns if necessary, and then run the migration:
So
Can I use this gem alongside standard Devise?
Yes! But you will need to enable the support of separate routes for standard Devise.
https://github.com/lynndylanhurley/devise_token_auth#can-i-use-this-gem-alongside-standard-devise
Personally, I wouldn't use Devise for your authentication but would create a custom one next to Devise just for your API. Devise can become a bit buggy later on in the process when using it for API-authentication. Then for your authorization you could use Pundit. You might want to use Regulator next to it for controller namespaced authorization polices(it's not under development anymore, but it does the job).
There's a nice tutorial about this process:
API Tutorial
Here you can find Pundit:
Pundit Gem
And here's the Regulator gem:
Regulator Gem

Where is the code that Devise generates?

I installed the Devise gem into my Rails app, and ran rails generate devise:install and rails generate devise User.
Without my doing anything, the url users/sign_up already has a view somehow. The problem is, I can't find the template that is being rendered anywhere. It's certainly not under app/views/users. I chose some text on the page and ran a search for it within my app, and got back 0 results.
Then I tried to sign up with the form, and got the following error:
NoMethodError in Devise::RegistrationsController#create
undefined method `current_sign_in_ip' for #<User:xxxxxxxxxxx>
I then searched for this controller, but there is no RegistrationsController in my app, and no Devise file. None of the files I'm looking for were generated by the two commands I mentioned above, either.
The Devise documentation doesn't seem to shed any light on where the Devise code is kept.
Is the code even in my app? I'm so confused.
Using Devise, you're able to generate the templates which Devise depends on for logins, password resets, etc. using the following command:
rails generate devise:views
This will create copies of the templates for Devise in your views directory.
For controllers, you can access/override their functionality by subclassing them in your own code. They're under the Devise namespace:
class NewRegistrationsController < Devise::RegistrationsController
# do stuff here
end
Then point the router to use this new controller:
devise_for :users, controllers: { registrations: 'new_registrations' }
The code for the controllers can be found in Devise's source code - you can reference it to better understand what each controller is doing.
Hope that helps!
This is standard practice for Rails "engines" (which almost all gems are) -
Think of them as libraries / dependencies... wherein they provide access to a lot of pre-compiled functionality through several hooks (often provided by an API).
One of the reasons I'd actually recommend people to write their own gem is because it helps you appreciate how the whole thing works. I wrote a gem, it uses views just like Devise:
These views are not seen in the application because they're appended to your Rails app at runtime. It's basically how the PATH var works in cmd, if you've ever had the pleasure of working with programmatic compilation etc.
Thus, Devise's "views" are stored in the Devise gem. This is appended to your Ruby installation... [Ruby install dir]/lib/ruby/gems/[ver]/gems, loaded at RunTime just like the PATH var...
Whilst you can generate your Devise views (as mentioned in the other answers), this is the base line of how it's able to access them without any prior references.
NoMethodError in Devise::RegistrationsController#create
undefined method `current_sign_in_ip' for #<User:xxxxxxxxxxx>
This means you don't have the current_sign_in_ip attribute for your Devise installation. I answered your question about this specifically here...
Devise error: undefined method `current_sign_in_ip'
All the devise MVC files are inside the gem. Below is my devise views directory. You could check yours as well. Go to your project root.
gem show 'devise'
/Users/saurabh/.rvm/gems/ruby-2.1.0/gems/devise-3.2.4
cd /Users/saurabh/.rvm/gems/ruby-2.1.0/gems/devise-3.2.4/app/views
You can generate views in your project if you wish to customize.
rails generate devise:views
All code of devise can be easily go through by devise and if you are using rubyMine you can view devise code in external libraries in devise folder.
To generate template for your model
rails generate devise:views
and then you can change your view as you want for your application.

Resources