Working on a course project and using rails 5 however the course isn't using rails 5 receiving this error after implementing this in NoMethodError in RegistrationsController#new
undefined method `<<' for nil:NilClass
was this before: class ApplicationController < ActionController::Base
class RegistrationsController < ApplicationController
before_action :configure_permitted_paramters, if: :devise_controller?
protected
def configure_permitted_paramters
devise_parameter_sanitizer.permit(:sign_up) << :fullname
devise_parameter_sanitizer.permit(:account_update) << :fullname << :cell_number << :license_plate_number << :vehicle_description << :email << :password
end
end
My routes changed to this as well part of the problem?
Rails.application.routes.draw do
root 'pages#home'
devise_for :users,
path => '',
:path_names => {:sign_in => 'login', :sign_out => 'logout', :edit => 'profile'},
controllers => {:omniauth_callbacks => 'omniauth_callbacks',
:registrations => 'registrations'
}
resources :users, only: [:show]
end
Your registration controller is redefining the Application Controller rather than defining the registration controller. Try the following:
class RegistrationsController < ApplicationController
def new
end
end
Related
I have the same problem as here undefined local variable or method `resource_class' in devise . The issue is that the code was already set like the answers. Is it a route problem ? Should I look elsewhere?
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :set_locale
before_action :set_instances
before_action :configure_permitted_parameters, if: :devise_controller?
before_action :mixpanel
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
end
def redirect_to_back
begin
redirect_to :back
rescue ActionController::RedirectBackError => e
redirect_to root_path
end
end
def set_instances
#new_event ||= Event.new(title: ".......", capacity: 50, start_date: Time.zone.now, start_time: Time.zone.now, end_time: Time.zone.now)
#featured_quizz ||= Questionnaire.where('featured IS TRUE')
end
helper_method :resource_name, :resource, :devise_mapping
protected
def resource_name
:user
end
def resource
#resource ||= User.new
end
def devise_mapping
#devise_mapping ||= Devise.mappings[:user]
end
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) << [:name, :firstname, :lastname, :phone]
devise_parameter_sanitizer.for(:account_update) << [:name, :firstname, :lastname, :image, :phone]
end
end
=> _links.html.slim
.row.text-center
.col-md-10.col-md-offset-1
- if devise_mapping.omniauthable?
- resource_class.omniauth_providers.each do |provider|
= link_to omniauth_authorize_path(resource_name, provider), class:"btn btn-fb btn-block callout callout-fb"
span.fa.fa-facebook-official<>
span Connectez vous avec #{provider.to_s.titleize}
undefined method `users_path' for #<#:0x007ff25695fce0>
Is it a syntax error in front ?
li = link_to "Votre Compte", resource, as: resource_name, url: session_path(resource_name), remote: true, data: { toggle: "modal", target: "#login-modal" }
or is it the block in my routes.rb =>
devise_for :users, :controllers => {
:registrations => "registrations",
:sessions => "sessions",
:confirmations => "confirmations",
:omniauth_callbacks => "callbacks" }
devise_for :admins
Thanks for your help
I don't think the problem is devise related : the Devise Readme makes no mention of creating routes for the model used in devise_for
the users_path helper method is enabled from config/routes.rb with the line :
get '/users', to: 'users#index', as: :users
or shortly, since you will probably need more routes :
resources :users, only: [:index] # you can skip the :only parameter if you need all restful routes.
Check Rails Routing from the Outside In for more information.
I'm just start to learn ruby and I'm writing a simple API, but I've got an error NoMethodError (undefined method moments' for nil:NilClass): app/controllers/api/v1/moments_controller.rb:8:inindex' Here is a code:
Rails.application.routes.draw do
resources :posts
devise_for :users
namespace :api do
namespace :v1 do
devise_scope :user do
post 'registrations' => 'registrations#create', :as => 'register'
post 'sessions' => 'sessions#create', :as => 'login'
delete 'sessions' => 'sessions#destroy', :as => 'logout'
end
get 'moments' => 'moments#index', :as => 'moments'
post 'moments' => 'moments#create'
end
end
devise_scope :user do
get '/users/sign_out' => 'devise/sessions#destroy'
end
end`
here code : moments_controller.rb
class Api::V1::MomentsController < ApplicationController
skip_before_filter :verify_authenticity_token,
:if => Proc.new { |c| c.request.format == 'application/json' }
before_filter :authenticate_user!
def index
#moments = current_user.moments
end
def create
#moment = current_user.moments.build(params[:moment])
if #moment.save
#moment
else
render :status => :unprocessable_entity,
:json => { :success => false,
:info => #moment.errors,
:data => {} }
end
end
#private
# def moment_params
# params.require(:moment).permit(:title, :completed)
# end
end
Can anyone see what is wrong with my code?
I am using rails 4 along with devise my routes.rb reads :
devise_for :users, :path => '',
:path_names => {:sign_in => 'login', :sign_out => 'logout', :sign_up => 'register'},
controllers: { registrations: "registrations", omniauth_callbacks: 'omniauth_callbacks' } do
get "/login" => "devise/sessions#new"
get "/signup" => "devise/registrations#new"
get "/logout" => "devise/sessions#destroy"
get "/login" => "devise/sessions#new"
end
resources :users, :only => [:show, :index], :path => "bloggers" do
resources :posts, :path => "articles"
end
Now when I create a new post as the currently signed in user (lets say the id is 1). The URL on the Post -> New action reads - > https://localhost:3000/bloggers/1/articles/new but I want to show
https://localhost:3000/articles/new, as the new action on the post should always be associated to the current_user.
I would imagine this being possible, but no clue how to do it.
Also a user has_many posts.
Help, please?
Authentication
Very simple actually:
#config/routes.rb
# If you have overridden "path_names", you don't need other paths for Devise
devise_for :users, :path => '',
:path_names => {:sign_in => 'login', :sign_out => 'logout', :sign_up => 'register'},
controllers: { registrations: "registrations", omniauth_callbacks: 'omniauth_callbacks' }
resources :posts, as: "articles", path: "articles"
#app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
before_action :authenticate_user!, except: :index
def index
#Stuff here
end
end
The bottom line here is that whenever you want to use an authenticated area (especially with Devise), you just need to "protect" the controller#actions which you want to limit access to. The beauty is that you can use the authenticate_user! helper method to make these work
Further to that, you will then be able to just call current_user in your controller (not having to set the user, as you're doing now):
#app/controllers/posts_controller.rb
class PostsController < ApplicationController
def new
#post = current_user.posts.new
end
def create
#post = current_user.posts.new post_params
#post.save
end
private
def post_params
params.require(:post).permit(:x, :y, :z)
end
end
basically I want to have two separate actions for change password and change email instead of just one.
I have updated my routes to point to my new controller which inherits from Devise::RegistrationsController.
My routes.rb:
devise_for :users, :controllers => { :registrations => "registrations" }
devise_scope :user do
get "/users/password" => "registrations#change_password", :as => :change_password
end
My registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
def change_password
end
end
My app/views/devise/registrations/change_password.html.erb
<%=debug resource%>
Which gives me nil.
What am I missing here?
Thanks!
In Devise's built-in registrations_controller.rb, there is an authenticate_scope! method that creates the resource object you're looking for. It is executed by a prepend_before_filter, but only for certain methods:
class Devise::RegistrationsController < DeviseController
...
prepend_before_filter :authenticate_scope!, :only => [:edit, :update, :destroy]`
So you simply need to tell your custom controller to run that filter on your change_password method:
class RegistrationsController < Devise::RegistrationsController
prepend_before_filter :authenticate_scope!, :only => [:change_password]
def change_password
end
end
class RegistrationsController < Devise::RegistrationsController
def change_password
super
#resource = resource
end
end
app/views/devise/registrations/change_password.html.erb
<%=debug #resource%>
I have a session controller which inherits from Devise::SessionsController:
class SessionsController < Devise::SessionsController
skip_before_filter :authenticate_user!, :only => [:get_token]
def create
....
end
def destroy
...
end
def get_token
response.headers["app-key"] = form_authenticity_token()
render :text=>'Token Set'
end
end
As you can see above i am overwriting create and destroy action and i have added another action named get_token. I added routes for it as shown below:
Routes.rb
Application.routes.draw do
devise_for :users, :controllers => { :sessions => "sessions" }, :path => "users", :path_names => { :sign_in => 'login', :sign_out => 'logout',:confirmation => 'verification'}
match 'get_token', :to => 'sessions#get_token'
But I am getting the following errror when i am trying to access get_token method;
[Devise] Could not find devise mapping for path "/get_token".
How to add route for the get_token action.
Thanks in advance
You need to scope the route in Devise like so:
devise_scope :user do
get 'get_token' => 'sessions#get_token'
end
That should allow you call http://your-url/get_token to access that action.