No route matches [POST] "/users/sign_up.user" - ruby-on-rails

I incorporated devise in my user model but when i try to sign up someone i get this error No route matches [POST] "/users/sign_up.user"
I have the devise views and controllers in my app too. But when i click the sign up button i get that error.
this is my routes.rb
Rails.application.routes.draw do
devise_for :users, controllers: { registrations: "users/registrations" }
#resources :users
#resources :admin
devise_scope :user do
root :to => 'devise/sessions#new'
end
end
and this is my form
Sign up
<%= form_for(resource, as: resource_name, url: new_user_registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<div class="field">
<%= f.label :password %>
<% if #validatable %>
<em>(<%= #minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
<%= render "users/shared/links" %>

I had this exact same problem, and struggled with what was up for a while. My solution might not have anything to do with your particular case, but worth a try.
In my view I had used erb's button_to tag to hit the new_user_registration_path:
<%= button_to 'Sign up', new_user_registration_path %>
this was sending a POST request when the route is a GET request. Changing the button_to tag to a link_to tag solved it:
<%= link_to 'Sign up', new_user_registration_path %>

Since you changed the route to:
devise_for :users, controllers: { registrations: "users/registrations" }
There's no need for the resource in the path you're using in your form. Simply take it out and it should look like this:
<%= form_for(resource, as: resource_name, url: new_user_registration_path) do |f| %>

Related

No route matches [POST] "users/sign_up"

new to Rails here. I am wondering if anyone can help me with the problem that I am having. So basically, I am trying to use devise to add user authentication to my app. But every time I tried to submit the signup form, a warning "No route matches [POST] "users/sign_up"" appears on the app.
Here are my routes.rb
Rails.application.routes.draw do
get 'home/index'
root 'home#index'
#devise_for :users, controllers: { sessions: 'user/sessions' }
devise_for :users, path: 'auth', path_names:
{ sign_in: 'login', sign_out: 'logout', password: 'secret', confirmation: 'verification',
unlock: 'unblock', registration: 'register', sign_up: 'cmon_let_me_in' }
devise_scope :user do
get 'sign_in', to: 'devise/sessions#new'
end
resources :workspaces do
resources :items
end
end
Also, my new.html.erb on the devise/registration looks like:
<h2>Sign up</h2>
<%= form_for(resource, as: resource_name, url: new_user_registration_path) do |f| %>
<%= render "devise/shared/error_messages", resource: resource %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, autocomplete: "email" %>
</div>
<div class="field">
<%= f.label :password %>
<% if #minimum_password_length %>
<em>(<%= #minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "new-password" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "new-password" %>
</div>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
<%= render "users/shared/links" %>
Thank you for your help.

How to signup in two different pages with devise

I used devise inthe login system.I want create two new html under the registrations, which is two different roles' pages. And I want to go to that page by choosing roles in registrations/new, which is the original signup file. What should I do next?
my files order in the view/user/registrations: enter image description here.
edit.html.erb
new.html.erb
new_librarian.html.erb
new_student.html.erb
I want to signup in the new two pages through registrations/new
<!-- what i changed in the registrations/new -->
<h2>Sign up</h2>
<p>Sign up as a Student<%= link_to 'Student', users_registrations_new_student_path, :method => 'get' %></p>
<p>Sign up as a Librarian<%= link_to 'Librarian', users_registrations_new_librarian_path, :method => 'get'%></p>
<!-- what is in the registrations/new_student -->
<h2>Sign up</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= render "devise/shared/error_messages", adminv: resource %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, autocomplete: "email" %>
</div>
<div class="field">
<%= f.label :password %>
<% if #minimum_password_length %>
<em>(<%= #minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "new-password" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "new-password" %>
</div>
<div class="field">
<%= f.label :select_role %><br />
<%= f.collection_select :role, User::ROLES, :to_s, :humanize %>
</div>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
<!-- what i changed in the routes.rb -->
get 'users/registrations/new_librarian'
get 'users/registrations/new_student'
Could not find devise mapping for path "/users/registrations/new_student". This may happen for two reasons:
1) You forgot to wrap your route inside the scope block. For example: devise_scope :user do get "/some/route" => "some_devise_controller" end
2) You are testing a Devise controller bypassing the router. If so, you can explicitly tell Devise which mapping to use: #request.env["devise.mapping"] = Devise.mappings[:user]
You can achieve this by overriding the devise registration controller. In your overrided registration controller, in the new method once the user is saved,
#app/config/routes.rb
devise_for :users, :controllers => {:registrations => "registrations"}
# app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
def new
if #user.save!
if #user.role == "librarian"
redirect_to librarian_profile_path
elsif #user.role == "student"
redirect_to student_profile_path
end
end
end
end
You can define librarian_profile_path and student_profile_path in your routes
#app/config/routes.rb
get 'librarian/new' => 'librarian_profile#new', :as => 'librarian_profile'
get 'student/new' => 'student_profile#new', :as => 'student_profile'

Error disabling sign-ups for Devise model - Devise | Rails 5.2

I'm working on a Rails 5.2 project with two Devise models (Admin and User). I've followed the suggestion on this Stack Overflow answer to disable sign-ups for a Devise model without breaking Devise's edit_admin_registration_path.
After applying the changes suggested in routes.rb, running rails routes shows the changes working as expected, however when going to edit an Admin (edit_admin_registration_path), i get the error:
NoMethodError in Admins::RegistrationsController#edit
undefined method `validatable?' for nil:NilClass
I suspect the issue is that the linked answer was not using multiple Devise models with scoped views and controllers.
Any ideas?
I've added this to routes.rb:
devise_scope :admin do
get "/sign_in" => "admins/sessions#new" # custom path to login/sign_in
get "/sign_up" => "admins/registrations#new", as: "new_admin_registration" # custom path to sign_up/registration
end
devise_for :admins, skip: [ :registrations ], path: 'admins', controllers: { sessions: 'admins/sessions', registrations: 'admins/registrations' }
as :admins do
get 'admins/edit' => 'admins/registrations#edit', as: 'edit_admin_registration'
put 'admins' => 'admins/registrations#update', as: 'admin_registration'
end
Form on view:
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), method: :put) do |f| %>
<%= devise_error_messages! %>
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, autocomplete: "email", class: "form-control" %>
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
<% end %>
<%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :password, autocomplete: "off", class: "form-control" %>
<% if #minimum_password_length %>
<em><%= #minimum_password_length %> characters minimum</em>
<% end %>
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off", class: "form-control" %>
<%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password, autocomplete: "off", class: "form-control" %>
<%= f.submit "Update", class: "btn btn-success" %>
<% end %>
Answer found:
Routes needed to be configured like so (as can be found in this Stack Overflow answer):
devise_for :admins, :skip => [:registrations], path: 'admins', controllers: { sessions: 'admins/sessions', registrations: 'admins/registrations' }
devise_scope :admin do
get 'admins/edit' => 'admins/registrations#edit', as: 'edit_admin_registration'
put 'admins' => 'admins/registrations#update', as: 'admin_registration'
end

NoMethodError in UserController#new

I keep getting the following error on my app from 7.2.1 on Michael Hartl Ruby Rails tutorial. For some reason it is not accepting a nil #user field in the signup form.
undefined method `users_path' for #<#<Class:0x007ff87141aef8>:0x007ff870cf0d30>
new.html.erb
<% provide(:title, "Sign up") %>
<h1>Sign up</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(#user) do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.email_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Create my account", class: "btn btn-primary" %>
<% end %>
</div>
</div>
User_controller.rb
class UserController < ApplicationController
def new
#user = User.new
end
def show
#user = User.find(params[:id])
end
end
Routes.rb
Rails.application.routes.draw do
resources :user
get 'user/new'
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'user#new'
You need to change resources :user to resources :users and define the index action in the controller by adding
def index
end

routes pro-blem in form ruby and rails

I have create a sing up form in ruby and rails
I have create a form
<%= form_for #user, url: {action: "new"} do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Password Confirmation" %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Create my account",class: "btn btn-large btn-primary" %>
<% end %>
and i have write down in routes file this
get '/signup', to: 'users#new'
But when i see the out put as a html this is looking like this
<form accept-charset="UTF-8" action="/users/new" class="new_user" id="new_user" method="post">
It is post method how can i define get and post both in route file and how can i use action singup ?
Please help me
Do this on the route:
match '/signup', to: 'users#new'

Resources