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
Related
I am trying out for basic login and registration in ruby on rails,
ISSUE: When I click register button from new.html I am getting GET request but I can see method="post" in page source of that html
I have posted my code below
new.html.erb
<form>
<%= form_for(#user) do |f| %>
<%= f.label :user_name %>
<%= f.text_field :user_name %></br>
<%= f.label :email_id %>
<%= f.text_field :email_id %></br>
<%= f.label :password %>
<%= f.password_field :password %></br>
<%= f.label :college %>
<%= f.text_field :college %></br>
<%= f.label :major %>
<%= f.text_field :major %></br>
<%= f.label :current_job %>
<%= f.text_field :current_job %></br>
<%= f.submit("Create Account",class: "btn btn-primary") %>
<% end %>
</form>
My index.html.erb code which is loaded while application starts
<div class="container">
<div class="row">
<div class="login">
<%= form_tag("/user/login",:method => :post) do %>
<%= label_tag(:EmailId) %>
<%= text_field_tag(:email, params[:email]) %></br>
<%= label_tag(:password) %>
<%= password_field_tag(:password, params[:password]) %></br>
<%= submit_tag("Log In",class: "btn btn-primary") %>
<%= submit_tag("Register",class: "btn btn-primary") %>
<% end %>
</div>
</div>
</div>
My controller code
class UsersController < ApplicationController
def index
#user = User.all
end
def login
print "In Sign in controller"
#user = User.new
if params[:commit] == 'Register'
print "inside Register class"
redirect_to '/users/new'
else
#user = User.find(params[:email_id])
if #user and user.authenticate(params[:password])
sessions[:userId] = #user.user_id
end
end
end
def new
puts "****Inside New Method******"
#user = User.new
end
def create
puts "****Inside create Method******"
end
private
def user_params
end
end
My Route code
Rails.application.routes.draw do
root 'users#index'
resources :users
post '/users/login' => 'users#login'
As per my understanding post request should hit create method, but get /users method is hitting. Please help me out regarding this
Why do you have nested forms in new.html.erb? Remove the first form tag
<form>
<%= form_for(#user) do |f| %>
You have a form tag inside another form tag. Remove the tag at the top of your form. <%= form_for(#user) %> takes care everything that's needed to build the correct form.
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| %>
I'm having issues loading a new registration page using Devise in my rails app. When I try to load the sign up page, I get the error:
undefined method `errors' for nil:NilClass
which seems to be caused by the following line:
<%= devise_error_messages! %>
What could I be doing wrong?
routes.rb:
devise_for :users, :controllers => {:registrations => :registrations, omniauth_callbacks: "users/omniauth_callbacks"}
devise_scope :user do
post 'registrations' => 'registrations#create', :as => 'register'
post 'sessions' => 'sessions#create', :as => 'login'
delete 'sessions' => 'sessions#destroy', :as => 'logout'
end
registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
# GET /user/sign_up
def new
build_resource({})
respond_with self.resource
end
The sign up link:
<%= link_to "Sign Up", new_registration_path(resource_name), :class=>"btn btn-mini", :style=>'float:left'%>
application_helper.rb (where resource_name is defined)
def resource_name
:user
end
def resource
#resource ||= User.new
end
def devise_mapping
#devise_mapping ||= Devise.mappings[:user]
end
the sign up view
<div class="signUpPage">
<div class="form_container">
<h3 class="form_heading sign_up">Sign up</h3>
<%= semantic_form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="formField"><%= f.label :username, :class=> "label label-info" %><br />
<%= f.text_field :username, :class=> "signup_username", :maxlength=>"15"%></div>
<div class="usernameValidation validation">Username is already taken</div>
<div class="formField"><%= f.label :email, :class=> "label label-info" %><br />
<%= f.email_field :email%></div>
<div class="emailValidation validation">Email is already taken</div>
<div class="formField"><%= f.label :password, :class=>"label label-info" %><br />
<%= f.password_field :password, :class=> "signup_password" %></div>
<div class="passwordValidation validation">Password must be at least 8 characters</div>
<div class="formField"><%= f.label :password_confirmation, :class=>"label label-info" %><br />
<%= f.password_field :password_confirmation %></div>
<div class="passwordConfirmationValidation validation">Passwords do not match</div>
<div><%= f.submit "Sign up", :class=>"btn btn-info signup_button" %></div>
<% end %>
<%= render "devise/shared/links" %>
</div>
</div>
This is the full error message:
NoMethodError in Registrations#new
Showing Website/app/views/devise/registrations/new.html.erb where line #12 raised:
undefined method `errors' for nil:NilClass
Extracted source (around line #12):
9:
10: <%= semantic_form_for(resource, :as => resource_name, :url => registration_path(resource_name), namespace: 'sign_up', html: {id: 'form_user_sign_up'}) do |f| %>
11:
12: <%= devise_error_messages! %>
13:
14: <div class="formField"><%= f.label :username, :class=> "label label-info" %><br />
15: <%= f.text_field :username, :class=> "signup_username", :maxlength=>"15"%></div>
Rails.root: /Website
app/views/devise/registrations/new.html.erb:12:in `block in _app_views_devise_registrations_new_html_erb___65153715043208623_70149577578880'
app/views/devise/registrations/new.html.erb:10:in `_app_views_devise_registrations_new_html_erb___65153715043208623_70149577578880'
app/controllers/registrations_controller.rb:11:in `new'
This error tells me - undefined method 'user_profiles_path', while my routes are like 'user_profile_path'. Profile is a singleton child resource of users. Not sure what is causing this error. The error is raised by <%= form_for [#user, #profile] do |f| %> in _form.html.erb.
Routes.rb:
devise_for :users, :path_names => { :sign_in => "login", :sign_up => "register" } do
get "/login", :to => "devise/sessions#new"
get "/register", :to => "devise/registrations#new"
get "/logout", :to => "devise/sessions#destroy"
get '/account' => 'devise/registrations#edit'
end
root :to => "questions#redirect_on_visit"
match 'home', :to => "questions#index"
resources :questions do
resources :question_responses
end
resources :users do
resource :profile
end
_form.html.erb:
<%= form_for [#user, #profile] do |f| %>
<%= f.error_messages %>
<div class="field">
<%= f.label :display_name, "Display Name" %><br />
<%= f.text_field :display_name, :size => "43" %><br />
</div>
<div class="field">
<%= f.label :current_location, "Current Location" %><br />
<%= f.text_field :current_location, :size => "43" %><br />
</div>
<div><%= f.label :nationality, "Nationality" %><br />
<%= f.collection_select :nationality, Profile::NATIONALITY, :include_blank => true %>
</div><br />
<div><%= f.label :home_place, "Home Place" %><br />
<%= f.collection_select :home_place, Profile::HOME_PLACE, :include_blank => true %>
</div><br />
<div><%= f.label :occupation, "Occupation" %><br />
<%= f.collection_select :occupation, Profile::OCCUPATION, :include_blank => true %>
</div><br />
<div><%= f.label :interest, "Interests" %><br />
<%= f.collection_select :interest, Profile::INTERESTS, :include_blank => true %>
</div><br />
<div><%= f.label :hobby, "Hobbies" %><br />
<%= f.collection_select :hobby, Profile::HOBBIES, :include_blank => true %>
</div><br />
<div class="field">
<%= f.label :bio, "Short Bio" %><br />
<%= f.text_area :bio, :size => "50x5" %>
</div>
<div class="submit">
<%= f.submit "Create Profile" %>
</div>
<% end %>
profiles_controller.rb:
class ProfilesController < ApplicationController
before_filter :find_user
def new
#profile = #user.build_profile
end
def edit
end
private
def find_user
#user = User.find(params[:user_id])
end
end
application.html.erb:
<% if user_signed_in? %>
<% if !current_user.try(:profile) %>
Signed in as<div><%= link_to current_user.email, new_user_profile_path(current_user.id) %></div><br /><br />
<% else %>
Signed in as<div><%= link_to current_user.email, edit_user_profile_path(current_user.id) %></div><br /><br />
<% end %>
Not you? <%= link_to "Sign out", logout_path %>
<% else %>
<%= link_to "Sign up", new_user_registration_path %> or <%= link_to "Sign in", new_user_session_path %>
<% end %>
Routes:
user_profile POST /users/:user_id/profile(.:format) profiles#create
new_user_profile GET /users/:user_id/profile/new(.:format) profiles#new
edit_user_profile GET /users/:user_id/profile/edit(.:format) profiles#edit
GET /users/:user_id/profile(.:format) profiles#show
PUT /users/:user_id/profile(.:format) profiles#update
DELETE /users/:user_id/profile(.:format)
Okay this error is a known bug as reported here: https://github.com/rails/rails/issues/1769
And I found the right way to specify URL.
form_for([#user, #profile], url: user_profile_path(#user))
Syntactically important to have braces and important that there is no space between form_for and parenthesis.
You can correct this by manually specifying the URL in the form:
<%= form_for [#user, #profile], :url => user_profile_path(#user) do |f| %>
I am making a Rails 3.1 app and have a signup form that was working fine, but I seemed to have changed something to break it.. I'm using Twitter bootstrap and twitter_bootstrap_form_for gem. I made some change that messed with the formatting of the form fields, but more importantly, when I submit the Sign Up form to create a new User, the information is showing up in the URL and looks like this:
EDIT: This is happening in the latest versions of Chrome and Firefox
http://localhost:3000/?utf8=%E2%9C%93&authenticity_token=UaKG5Y8fuPul2Klx7e2LtdPLTRepBxDM3Zdy8S%2F52W4%3D&user%5Bemail%5D=kevinc%40example.com&user%5Bpassword%5D=testing&user%5Bpassword_confirmation%5D=testing&commit=Sign+Up
Here is the code for the form:
<div class="span7">
<h3 class="center" id="more">Sign Up Now!</h3>
<%= twitter_bootstrap_form_for #user do |user| %>
<%= user.email_field :email, :placeholder => 'me#example.com' %>
<%= user.password_field :password %>
<%= user.password_field :password_confirmation, 'Confirm Password' %>
<%= user.actions do %>
<%= user.submit 'Sign Up' %>
<% end %>
<% end %>
</div>
Here is the code for the UsersController:
class UsersController < ApplicationController
def new
#user = User.new
end
def create
#user = User.new(params[:user])
if #user.save
redirect_to about_path, :notice => "Signed up!"
else
render 'new'
end
end
end
Not sure if there is more you need but if so let me know! Thank you!
Edit: For debugging I tried specifying :post and also using a plain form_for
<%= form_for(#user, :method => :post) do |f| %>
<div class="field">
<%= f.label :email %>
<%= f.email_field :email %>
</div>
<div class="field">
<%= f.label :password %>
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
</div>
<div class="actions"><%= f.submit "Sign Up" %></div>
<% end %>
This gives me the same problem as above.
Adding routes.rb:
Auth31::Application.routes.draw do
get "home" => "pages#home"
get "about" => "pages#about"
get "contact" => "pages#contact"
get "help" => "pages#help"
get "login" => "sessions#new", :as => "login"
get "logout" => "sessions#destroy", :as => "logout"
get "signup" => "users#new", :as => "signup"
root :to => "pages#home"
resources :pages
resources :users
resources :sessions
resources :password_resets
end
The problem was that I was specifying the HTML form tag when Rails form_for generates its own. See below to add a class to a form:
<%= form_for(#user, :html => { :class => "form-stacked"} ) do |f| %>
This worked fine. I'm still not sure why having 2 tags would generate a GET request.
It looks like your for is sending a GET request, rather that a POST request. I'm not sure why this is happening (have you tried using plain form_for for debugging purposes?), but you should be able to fix it my explicitly setting the method:
<%= twitter_bootstrap_form_for(#user, :method => :post) do |user| %>