trying to add new users , using the devise gem
ran rake db:migrate and ran the rails server again
but when i type in 3000/posts/sign_up
it gives me the default sign up screen but when try to create a user i get
NoMethodError in Devise::RegistrationsController#create
undefined method `encrypted_password=' for #
my logs show
Started POST "/posts" for 127.0.0.1 at 2014-03-18 20:38:22 +0000
Processing by Devise::RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"2uO2ttqAfK1a2C855cZOpDTsS2Duc7ZzVJxAQ5ObL8M=", "post"=>{"email"=>"neil#example.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
Completed 500 Internal Server Error in 79ms
NoMethodError (undefined method `encrypted_password=' for #<Post:0x007fde3e09e538>):
activemodel (4.0.4) lib/active_model/attribute_methods.rb:439:in `method_missing'
activerecord (4.0.4) lib/active_record/attribute_methods.rb:167:in `method_missing'
devise (3.2.2) lib/devise/models/database_authenticatable.rb:42:in `password='
activerecord (4.0.4) lib/active_record/attribute_assignment.rb:42:in `public_send'
activerecord (4.0.4) lib/active_record/attribute_assignment.rb:42:in `_assign_attribute'
activerecord (4.0.4) lib/active_record/attribute_assignment.rb:29:in `block in assign_attributes'
activerecord (4.0.4) lib/active_record/attribute_assignment.rb:23:in `each'
activerecord (4.0.4) lib/active_record/attribute_assignment.rb:23:in `assign_attributes'
new.html.erb
<h2>Sign up</h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :email %><br />
<%= f.email_field :email, :autofocus => true %></div>
<div><%= f.label :password %><br />
<%= f.password_field :password %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>
<div><%= f.submit "Sign up" %></div>
<% end %>
<%= render "devise/shared/links" %>
post.rb
class Post < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
rake routes
Prefix Verb URI Pattern Controller#Action
new_post_session GET /posts/sign_in(.:format) devise/sessions#new
post_session POST /posts/sign_in(.:format) devise/sessions#create
destroy_post_session DELETE /posts/sign_out(.:format) devise/sessions#destroy
post_password POST /posts/password(.:format) devise/passwords#create
new_post_password GET /posts/password/new(.:format) devise/passwords#new
edit_post_password GET /posts/password/edit(.:format) devise/passwords#edit
PATCH /posts/password(.:format) devise/passwords#update
PUT /posts/password(.:format) devise/passwords#update
cancel_post_registration GET /posts/cancel(.:format) devise/registrations#cancel
post_registration POST /posts(.:format) devise/registrations#create
new_post_registration GET /posts/sign_up(.:format) devise/registrations#new
edit_post_registration GET /posts/edit(.:format) devise/registrations#edit
PATCH /posts(.:format) devise/registrations#update
PUT /posts(.:format) devise/registrations#update
DELETE /posts(.:format) devise/registrations#destroy
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
about GET /about(.:format) pages#about
root GET / pages#welcome
rails g
Neils-MacBook-Pro:prospects neilpatel$ rails g devise:views
invoke Devise::Generators::SharedViewsGenerator
create app/views/devise/shared
create app/views/devise/shared/_links.erb
invoke form_for
create app/views/devise/confirmations
create app/views/devise/confirmations/new.html.erb
create app/views/devise/passwords
create app/views/devise/passwords/edit.html.erb
create app/views/devise/passwords/new.html.erb
create app/views/devise/registrations
create app/views/devise/registrations/edit.html.erb
create app/views/devise/registrations/new.html.erb
create app/views/devise/sessions
create app/views/devise/sessions/new.html.erb
create app/views/devise/unlocks
create app/views/devise/unlocks/new.html.erb
invoke erb
create app/views/devise/mailer
create app/views/devise/mailer/confirmation_instructions.html.erb
create app/views/devise/mailer/reset_password_instructions.html.erb
create app/views/devise/mailer/unlock_instructions.html.erb
Neils-MacBook-Pro:prospects neilpatel$ rails generate devise Post
invoke active_record
create db/migrate/20140318202353_add_devise_to_posts
insert app/models/post.rb
route devise_for :posts
Neils-MacBook-Pro:prospects neilpatel$ rake db:migrate
The main problem is here
NoMethodError (undefined method `encrypted_password=' for #<Post:0x007fde3e09e538>):
Check out you migration file. Is encrypted_password present or not? Do you have this t.string :encrypted_password, :null => false, :default => "" line of code in your migration file. And in your model file (i.e post.rb) you haven't define password attribute too.
Related
Given I am a registered user
And I am not logged in
When I visit "http://localhost:3000/member/password/edit?reset_password_token=BVE492WU1YqMp6nmxCX4"
And I fill in "asdfasdf" in "user[:password]
And I fill in "asdfasdf" in "user[:password_confirmation]
And I submit form
Then I see "Email can't be blank" -> which should be skipped
routes.rb
devise_for :users,
path: 'member',
controllers: {registrations: 'member/registrations',
sessions: 'member/sessions',
passwords: 'member/passwords',
confirmations: 'member/confirmations',
unlocks: 'member/unlocks',
omniauth_callbacks: 'member/omniauth_callbacks'}
devise_scope :user do
match 'member/finish_signup/:id', to: 'member/registrations#finish_signup', via: [:get, :post], :as => :finish_signup
match 'member/show_token', to: 'member/registrations#show_token', via: [:get, :post]
# also tried without the following lines
patch 'member/password' => 'member/passwords#update'
put 'member/password' => 'member/passwords#update'
end
application_controller.rb
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_in, keys: [:email, :password])
devise_parameter_sanitizer.permit(:account_update, keys: [:email, :password, :password_confirmation])
# NONE of the both below change anything
devise_parameter_sanitizer.permit(:sign_up, keys: [:email, :password, :password_confirmation])
# this one does not work anymore
# devise_parameter_sanitizer.for(:sign_up) do |u|
# u.permit(:email,:password,:password_confirmation)
# end
end
passwords/edit.html.erb
<%= form_for(resource, as: resource_name, url: password_path(resource_name), method: :put) do |f| %>
<%= devise_error_messages! %>
<%= f.hidden_field :reset_password_token %>
<% if #minimum_password_length %>
<em>(<%= #minimum_password_length %> characters minimum)</em><br/>
<% end %>
<%= f.password_field :password, autofocus: true, autocomplete: "off" %>
<%= f.password_field :password_confirmation, autocomplete: "off" %>
<%= f.submit "Change my password" %>
<% end %>
also tried this:
<%= form_for(resource, as: resource_name, url: member_password_path(resource_name), html: {method: :put}) do |f| %>
The rendered HTML is which looks good (post method):
<form class="new_user" id="new_user" action="/member/password.user" accept-charset="UTF-8" method="post">
<input name="utf8" type="hidden" value="✓"><input type="hidden" name="_method" value="patch">
<input type="hidden" name="authenticity_token" value="Egh7QFhtHwcBVCuUgTDFnOF2C/g2Ql/sKeiuwjchjmpDh4JqyBPuLTaN9VT9iFFiP7JDxbp0+BeeA2QUSJWN3g==">
<input type="hidden" value="BVE492WU1YqMp6nmxCX4" name="user[reset_password_token]" id="user_reset_password_token">
<em>(8 characters minimum)</em><br>
<input autofocus="autofocus" autocomplete="off" type="password" name="user[password]" id="user_password">
<input autocomplete="off" type="password" name="user[password_confirmation]" id="user_password_confirmation">
<input type="submit" name="commit" value="Change my password" data-disable-with="Change my password">
</form>
member/passwords_controller.rb
class Member::PasswordsController < Devise::PasswordsController
end
rake routes:
new_user_session GET /member/sign_in(.:format) member/sessions#new
user_session POST /member/sign_in(.:format) member/sessions#create
destroy_user_session GET /member/sign_out(.:format) member/sessions#destroy
user_omniauth_authorize GET|POST /member/auth/:provider(.:format) member/omniauth_callbacks#passthru {:provider=>/facebook/}
user_omniauth_callback GET|POST /member/auth/:action/callback(.:format) member/omniauth_callbacks#(?-mix:facebook)
user_password POST /member/password(.:format) member/passwords#create
new_user_password GET /member/password/new(.:format) member/passwords#new
edit_user_password GET /member/password/edit(.:format) member/passwords#edit
PATCH /member/password(.:format) member/passwords#update
PUT /member/password(.:format) member/passwords#update
cancel_user_registration GET /member/cancel(.:format) member/registrations#cancel
user_registration POST /member(.:format) member/registrations#create
new_user_registration GET /member/sign_up(.:format) member/registrations#new
edit_user_registration GET /member/edit(.:format) member/registrations#edit
PATCH /member(.:format) member/registrations#update
PUT /member(.:format) member/registrations#update
DELETE /member(.:format) member/registrations#destroy
user_confirmation POST /member/confirmation(.:format) member/confirmations#create
new_user_confirmation GET /member/confirmation/new(.:format) member/confirmations#new
GET /member/confirmation(.:format) member/confirmations#show
user_unlock POST /member/unlock(.:format) member/unlocks#create
new_user_unlock GET /member/unlock/new(.:format) member/unlocks#new
GET /member/unlock(.:format) member/unlocks#show
finish_signup GET|POST /member/finish_signup/:id(.:format) member/registrations#finish_signup
member_show_token GET|POST /member/show_token(.:format) member/registrations#show_token
member_password PATCH /member/password(.:format) member/passwords#update
PUT /member/password(.:format) member/passwords#update
And this is the log when I submit the filled out "/passwords/edit.html.erb"
(http://localhost:3000/member/password/edit?reset_password_token=BVE492WU1YqMp6nmxCX4)
Started GET "/member/password/edit?reset_password_token=[FILTERED]" for 127.0.0.1 at 2016-03-04 12:40:41 +0100
Processing by Member::PasswordsController#edit as HTML
Parameters: {"reset_password_token"=>"[FILTERED]"}
DEPRECATION WARNING: [Devise] Changing the sanitized parameters through "Devise::ParameterSanitizer#for(sign_up) is deprecated and it will be removed from Devise 4.1.
Please use the `permit` method:
devise_parameter_sanitizer.permit(:sign_up) do |user|
# Your block here.
end
(called from deprecate_for_with_block at /Users/jan/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/devise-4.0.0.rc1/lib/devise/parameter_sanitizer.rb:177)
Rendered devise/shared/_links.html.erb (1.6ms)
Rendered devise/passwords/edit.html.erb within layouts/application (3.5ms)
Completed 200 OK in 14ms (Views: 11.5ms)
Incoming Headers:
Origin: http://localhost:3000
Access-Control-Request-Method:
Access-Control-Request-Headers:
Started POST "/member/password.user" for 127.0.0.1 at 2016-03-04 12:41:19 +0100
Processing by Member::PasswordsController#create as
Parameters: {"utf8"=>"✓", "authenticity_token"=>"gmyAZDVZR4Z/OO3oa934MzNUwCFnzDSd7QLo6ZCoAnjT43lOpSe2rEjhMygXZWzN7ZCIHOv6k2Za6SI/7xwBzA==", "user"=>{"reset_password_token"=>"[FILTERED]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Change my password"}
Completed 406 Not Acceptable in 2ms
ActionController::UnknownFormat (ActionController::UnknownFormat):
responders (2.1.1) lib/action_controller/respond_with.rb:207:in `respond_with'
devise (4.0.0.rc1) app/controllers/devise/passwords_controller.rb:19:in `create'
/Users/jan/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/bundler/gems/rails-c901fad42cb4/actionpack/lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
/Users/jan/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/bundler/gems/rails-c901fad42cb4/actionpack/lib/abstract_controller/base.rb:183:in `process_action'
/Users/jan/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/bundler/gems/rails-c901fad42cb4/actionpack/lib/action_controller/metal/rendering.rb:30:in `process_action'
/Users/jan/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/bundler/gems/rails-c901fad42cb4/actionpack/lib/abstract_controller/callbacks.rb:20:in `block in process_action'
/Users/jan/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/bundler/gems/rails-c901fad42cb4/activesupport/lib/active_support/callbacks.rb:126:in `call'
/Users/jan/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/bundler/gems/rails-c901fad42cb4/activesupport/lib/active_support/callbacks.rb:126:in `call'
/Users/jan/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/bundler/gems/rails-c901fad42cb4/activesupport/lib/active_support/callbacks.rb:506:in `blo
I think the routing from password_path(resource_name) is incorrect. It brings me to member/passwords#create instead of member/passwords#update which seems to be right.
Please, how can I fix this?
.gemfile
gem 'rails', github: "rails/rails"
gem 'active_model_serializers', github: "rails-api/active_model_serializers", branch: 'master'
gem 'mongoid', git: 'https://github.com/estolfo/mongoid.git', branch: 'MONGOID-4218-rails-5'
# api token authentication for devise
gem 'devise', "4.0.0.rc1"
# we can switch back when Rails 5, and Devise 4 is supported
gem 'simple_token_authentication', github: "consti/simple_token_authentication"
gem 'bootstrap_form'
gem 'omniauth'
gem 'omniauth-facebook'
Changing the as: name and the of the route and the method to post did the trick. It seems that they got not overridden in my devise_scope.
'member/password/update' => 'member/passwords#update', as: :member_password_custom_update
I am having a problem with showing any of the views for my resource Field. I have this kind of association: User has one farm, farm has many fields.
My models:
User.rb
has_one :farm
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:token_authenticatable, :confirmable, :lockable, :timeoutable
attr_accessible :email, :password, :password_confirmation, :remember_me, :username
--
Farm.rb
belongs_to :user
has_many :fields
attr_accessible :name, :contact, :adress, :user_id
--
Field.rb
belongs_to :farm
attr_accessible :crop, :longitude, :latitude, :occupied, :farm_id
My rake routes:
user_farm_fields GET /users/:user_id/farm/:farm_id/fields(.:format)
{:action=>"index", :controller=>"fields"}
POST /users/:user_id/farm/:farm_id/fields(.:format)
{:action=>"create", :controller=>"fields"}
new_user_farm_field GET /users/:user_id/farm/:farm_id/fields/new(.:format) {:action=>"new", :controller=>"fields"}
edit_user_farm_field GET /users/:user_id/farm/:farm_id/fields/:id/edit(.:format){:action=>"edit", :controller=>"fields"}
user_farm_field GET /users/:user_id/farm/:farm_id/fields/:id(.:format) {:action=>"show", :controller=>"fields"}
PUT /users/:user_id/farm/:farm_id/fields/:id(.:format) {:action=>"update", :controller=>"fields"}
DELETE /users/:user_id/farm/:farm_id/fields/:id(.:format) {:action=>"destroy", :controller=>"fields"}
user_farms GET /users/:user_id/farm(.:format)
{:action=>"index", :controller=>"farms"}
POST /users/:user_id/farm(.:format)
{:action=>"create", :controller=>"farms"}
new_user_farm GET /users/:user_id/farm/new(.:format)
{:action=>"new", :controller=>"farms"}
edit_user_farm GET /users/:user_id/farm/:id/edit(.:format)
{:action=>"edit", :controller=>"farms"}
user_farm GET /users/:user_id/farm/:id(.:format)
{:action=>"show", :controller=>"farms"}
PUT /users/:user_id/farm/:id(.:format)
{:action=>"update", :controller=>"farms"}
DELETE /users/:user_id/farm/:id(.:format)
{:action=>"destroy", :controller=>"farms"}
users GET /users(.:format)
{:action=>"index", :controller=>"users"}
POST /users(.:format)
{:action=>"create", :controller=>"users"}
new_user GET /users/new(.:format)
{:action=>"new", :controller=>"users"}
edit_user GET /users/:id/edit(.:format)
{:action=>"edit", :controller=>"users"}
user GET /users/:id(.:format)
{:action=>"show", :controller=>"users"}
PUT /users/:id(.:format)
{:action=>"update", :controller=>"users"}
DELETE /users/:id(.:format)
{:action=>"destroy", :controller=>"users"}
new_user_session GET /accounts/sign_in(.:format)
{:action=>"new", :controller=>"devise/sessions"}
user_session POST /accounts/sign_in(.:format)
{:action=>"create", :controller=>"devise/sessions"}
destroy_user_session GET /accounts/sign_out(.:format)
{:action=>"destroy", :controller=>"devise/sessions"}
user_password POST /accounts/password(.:format)
{:action=>"create", :controller=>"devise/passwords"}
new_user_password GET /accounts/password/new(.:format)
{:action=>"new", :controller=>"devise/passwords"}
edit_user_password GET /accounts/password/edit(.:format)
{:action=>"edit", :controller=>"devise/passwords"}
PUT /accounts/password(.:format)
{:action=>"update", :controller=>"devise/passwords"}
cancel_user_registration GET /accounts/cancel(.:format)
{:action=>"cancel", :controller=>"devise/registrations"}
user_registration POST /accounts(.:format)
{:action=>"create", :controller=>"devise/registrations"}
new_user_registration GET /accounts/sign_up(.:format)
{:action=>"new", :controller=>"devise/registrations"}
edit_user_registration GET /accounts/edit(.:format)
{:action=>"edit", :controller=>"devise/registrations"}
PUT /accounts(.:format)
{:action=>"update", :controller=>"devise/registrations"}
DELETE /accounts(.:format)
{:action=>"destroy", :controller=>"devise/registrations"}
user_confirmation POST /accounts/confirmation(.:format)
{:action=>"create", :controller=>"devise/confirmations"}
new_user_confirmation GET /accounts/confirmation/new(.:format)
{:action=>"new", :controller=>"devise/confirmations"}
GET /accounts/confirmation(.:format)
{:action=>"show", :controller=>"devise/confirmations"}
user_unlock POST /accounts/unlock(.:format)
{:action=>"create", :controller=>"devise/unlocks"}
new_user_unlock GET /accounts/unlock/new(.:format)
{:action=>"new", :controller=>"devise/unlocks"}
GET /accounts/unlock(.:format)
{:action=>"show", :controller=>"devise/unlocks"}
home_index GET /home/index(.:format)
{:controller=>"home", :action=>"index"}
root /
{:controller=>"home", :action=>"index"}
My form for making new field:
<%= form_for([#user, #farm, #field]) do |f| %>
<% if #field.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#field.errors.count, "error") %> prohibited this field from being saved:</h2>
<ul>
<% #field.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :crop %><br />
<%= f.text_field :crop %>
</div>
<div class="field">
<%= f.label :longitude %><br />
<%= f.text_field :longitude %>
</div>
<div class="field">
<%= f.label :latitude %><br />
<%= f.text_field :latitude %>
</div>
<div class="field">
<%= f.label :occupied %><br />
<%= f.check_box :occupied %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
And my corresponding new method for field:
def new
#farm = Farm.where("user_id = ?", current_user).first
#user = current_user
#field = Field.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #field }
end
My routes.rb:
resources :users do
resources :farms, :path => 'farm' do
resources :fields
end
end
devise_for :users, :path => 'accounts'
After I try going to this link for creating new field :
<%= link_to 'New Field', new_user_farm_field_path(#user, #farm, #field) %>
I get this message:
undefined method `user_fields_path' for #<#<Class:0x496e1b0>:0x496b6d8>
On the surface level, the problem is likely to do with the form_for helper trying to set the url for your form without the correct nesting structure
You may wish to try this:
#app/views/fields/new.html.erb
<%= form_for [#user, #farm, #field], url: user_farm_fields_path do |f| %> #-> the create path
--
Nesting
There may be some deeper issues for you:
The corresponding route helper would be publisher_magazine_photo_url,
requiring you to specify objects at all three levels. Indeed, this
situation is confusing enough that a popular article by Jamis Buck
proposes a rule of thumb for good Rails design:
Resources should never be nested more than 1 level deep.
The caveat here, is that the docs stipulate you can do what you're doing (deep nesting) (I originally thought you shouldn't). Anyway, I think you might want to consider making your routes more "shallow", as to ensure they are more manageable.
To do this, I'd like you to consider the role of user in what you're doing:
User has_one Farm
This means that when you call resources :farms, you've got an antipattern - how can you select a farm if a user only has one? Surely a better way would be to remove any dependence on the user, create a singular resource for :farm, and then set the fields flow as you had them before
Here's how I'd do it:
#config/routes.rb
root "farms#index"
resources :farms, only: :index
resource :farm do #-> domain.com/farm (authentictable)
resources :fields #-> domain.com/farm/fields/new
end
#app/controllers/farms_controller.rb
Class FarmsController < ApplicationController
before_action :authenticate_user!, except: :index
def show
#farm = current_user.farm
end
def index
#farms = Farm.all
end
end
#app/models/user.rb
Class User < ActiveRecord::Base
has_one :farm
before_create :build_farm #-> creates farm if not already built
end
This will give you the ability to work everything back from being connected to the current_user:
#app/views/fields/new.html.erb
<%= form_for [#farm, #field] do |f| %>
<%= f.text_area :field_name %>
<%= f.submit %>
<% end %>
#app/controllers/fields_controller.rb
Class FieldsController < ApplicationController
before_action :authenticate_user!
def new
#farm = current_user.farm
#field = #farm.fields.new
end
def create
#farm = current_user.farm
#field = #farm.fields.new field_params
end
private
def field_params
params.require(:field).permit(:field_name)
end
end
I've been struggling for a while with this one. I have a Rails4/Devise 3.1 app with two users in the system:
Graduates
Employers
and one devise User who can be either a Graduate or Employer via a polymorphic :profile association. I have Graduates sign up via /graduate/sign_up path and employers via /employer/sign_up path both of which route to the same /views/devise/registrations/new.html.erb view (since their signup form is pretty much the same - email and password). Everything works fine, except when there is a validation error, RegistrationsController#create.respond_with resource always redirects both user types to /users path and I need to redirect them back to where they originally came from, e.g. /graduate/sign_up or /employer/sign_up respectively. I've tried replacing respond_with with redirect_to, but I end up loosing the resource object with associated error messages. Any idea guys on how this can be done?
These are my models:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
belongs_to :profile, polymorphic: true
end
class Graduate < ActiveRecord::Base
has_one :user, as: :profile
end
class Employer < ActiveRecord::Base
has_one :user, as: :profile
end
Registrations controller:
class RegistrationsController < Devise::RegistrationsController
def create
build_resource sign_up_params
user_type = params[:user][:user_type]
# This gets set to either 'Graduate' or 'Employer'
child_class_name = user_type.downcase.camelize
resource.profile = child_class_name.constantize.new
if resource.save
if resource.active_for_authentication?
set_flash_message :notice, :signed_up if is_navigational_format?
sign_up(resource_name, resource)
respond_with resource, :location => after_sign_up_path_for(resource)
else
set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
expire_session_data_after_sign_in!
respond_with resource, :location => after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords(resource)
respond_with resource
end
end
end
Registration view (same for both, graduates and employers):
<h2>Sign up</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :email %><br />
<%= f.email_field :email, :autofocus => true %></div>
<div><%= f.label :password %><br />
<%= f.password_field :password %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>
<%= hidden_field resource_name, :user_type, value: params[:user][:user_type] %>
<div><%= f.submit "Sign up" %></div>
<% end %>
<%= render "devise/shared/links" %>
These are my routes:
devise_for :users, :controllers => { :registrations => 'registrations' }
devise_scope :user do
match 'graduate/sign_up', to: 'registrations#new', user: { user_type: 'graduate' }, via: [:get]
match 'employer/sign_up', to: 'registrations#new', user: { user_type: 'employer' }, via: [:get]
end
root to: 'home#index'
Output of rake routes;
$ rake routes
Prefix Verb URI Pattern Controller#Action
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) registrations#cancel
user_registration POST /users(.:format) registrations#create
new_user_registration GET /users/sign_up(.:format) registrations#new
edit_user_registration GET /users/edit(.:format) registrations#edit
PATCH /users(.:format) registrations#update
PUT /users(.:format) registrations#update
DELETE /users(.:format) registrations#destroy
graduate_sign_up GET /graduate/sign_up(.:format) registrations#new {:user=>{:user_type=>"graduate"}}
employer_sign_up GET /employer/sign_up(.:format) registrations#new {:user=>{:user_type=>"employer"}}
root GET /
Apparently, the :location parameter is ignored if you have an existing template called, well, in this case, #index. I still don't understand why Devise redirects to #index or #show when the resource has errors on it.
I'll update this answer as I find out more.
Calling respond_with will render the default action of that resource. This I believe is the index action (/users/) and thus the cause for the redirect.
I think what you want to do is render the new action instead. Try the following:
if resource.save
...
else
clean_up_passwords(resource)
render :action => 'new'
end
In case of respond_with, for an html response - if the request method is get, an exception is raised but for other requests such as post the response depends on whether the resource has any validation errors (i.e. assuming that an attempt has been made to save the resource, e.g. by a create action) -
If there are no errors, i.e. the resource was saved successfully, the
response redirect's to the resource i.e. its show action.
If there are validation errors, the response renders a default action,
which is :new for a post request or :edit for patch or put.
source: http://apidock.com/rails/v4.1.8/ActionController/MimeResponds/respond_with
For your case, something like render 'view_path' in place of respond_with block should work.
I am using the devise gem, and I have had user registration working in the past. However, something seems to have broken.
Mavens::Application.routes.draw do
devise_for :users
resources :events
resources :periods
resources :products
resources :cart_rows
resources :product_requests
resources :inqueries
match '/profile', to: 'static_pages#profile'
# resources :registrations do
# member do
# post :save_period
# end
# end
root :to => 'static_pages#home'
get "static_pages/home"
get "static_pages/about"
end
I should have used a different name than "registrations" but this registration is for an event, not a user. I've commented that out thinking it had something to do with this breaking.
Here is my rake routes:
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
events GET /events(.:format) events#index
POST /events(.:format) events#create
new_event GET /events/new(.:format) events#new
edit_event GET /events/:id/edit(.:format) events#edit
event GET /events/:id(.:format) events#show
PUT /events/:id(.:format) events#update
DELETE /events/:id(.:format) events#destroy
periods GET /periods(.:format) periods#index
POST /periods(.:format) periods#create
new_period GET /periods/new(.:format) periods#new
edit_period GET /periods/:id/edit(.:format) periods#edit
period GET /periods/:id(.:format) periods#show
PUT /periods/:id(.:format) periods#update
DELETE /periods/:id(.:format) periods#destroy
products GET /products(.:format) products#index
POST /products(.:format) products#create
new_product GET /products/new(.:format) products#new
edit_product GET /products/:id/edit(.:format) products#edit
product GET /products/:id(.:format) products#show
PUT /products/:id(.:format) products#update
DELETE /products/:id(.:format) products#destroy
cart_rows GET /cart_rows(.:format) cart_rows#index
POST /cart_rows(.:format) cart_rows#create
new_cart_row GET /cart_rows/new(.:format) cart_rows#new
edit_cart_row GET /cart_rows/:id/edit(.:format) cart_rows#edit
cart_row GET /cart_rows/:id(.:format) cart_rows#show
PUT /cart_rows/:id(.:format) cart_rows#update
DELETE /cart_rows/:id(.:format) cart_rows#destroy
product_requests GET /product_requests(.:format) product_requests#index
POST /product_requests(.:format) product_requests#create
new_product_request GET /product_requests/new(.:format) product_requests#new
edit_product_request GET /product_requests/:id/edit(.:format) product_requests#edit
product_request GET /product_requests/:id(.:format) product_requests#show
PUT /product_requests/:id(.:format) product_requests#update
DELETE /product_requests/:id(.:format) product_requests#destroy
inqueries GET /inqueries(.:format) inqueries#index
POST /inqueries(.:format) inqueries#create
new_inquery GET /inqueries/new(.:format) inqueries#new
edit_inquery GET /inqueries/:id/edit(.:format) inqueries#edit
inquery GET /inqueries/:id(.:format) inqueries#show
PUT /inqueries/:id(.:format) inqueries#update
DELETE /inqueries/:id(.:format) inqueries#destroy
profile /profile(.:format) static_pages#profile
root / static_pages#home
static_pages_home GET /static_pages/home(.:format) static_pages#home
static_pages_about GET /static_pages/about(.:format) static_pages#about
And here is the view page:
<h2>Sign Up</h2>
<%= link_to "Sign in", new_session_path(resource_name) %> | <%= link_to "Forgot your password?", new_password_path(resource_name) %><br />
<br />
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<%= f.label :email %>
<%= f.email_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
<%= f.label :first_name %>
<%= f.text_field :first_name%>
<%= f.label :last_name %>
<%= f.text_field :last_name%>
<%= f.label :license_number %>
<%= f.text_field :license_number %>
<%= f.label :state %>
<%= f.select :state, User::STATES%>
<%= f.label :specialty %>
<%= f.select :specialty, User::SPECIALTY%>
<br />
<%= f.submit "Sign up" %>
<% end %>
Any ideas on what could have changed to break the routing?
I did a rails d on the model and controller and that seemed to get it working. Pro_tip, always prefix variable names with something so they don't compete with names of other systems you pull in!
I've gone through the getting started with rails tutorial and now i'm trying to setup another project using the devise gem for authentication. Basically all i want is to have a home page, a page where the user can fill out a form to essentially signup, and then a secure page that does the authentication.
right now i'm having problems when i navigate to the signup page. Here's the error i'm receiving:
NoMethodError in Signup#index
Showing /Users/tomcaflisch/Sites/project2/app/views/signup/_form.html.erb where line #1 raised:
undefined method `users_path' for #<#<Class:0x007fa734b3e510>:0x007fa734b3a910>
Extracted source (around line #1):
1: <%= form_for(#user) do |f| %>
2: <% if #user.errors.any? %>
3: <div id="errorExplanation">
4: <h2><%= pluralize(#user.errors.count, "error") %> prohibited this post from being saved: </h2>
signup_controller.rb:
class SignupController < ApplicationController
def index
#user = User.new
end
def new
#user = User.new
respond_to do |format|
format.html
end
end
end
user.rb model:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :username
end
../views/signup_form.html.erb:
<%= form_for(#user) do |f| %>
<% if #user.errors.any? %>
<div id="errorExplanation">
<h2><%= pluralize(#user.errors.count, "error") %> prohibited this post from being saved: </h2>
<ul>
<% #user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :username %><br />
<%= f.text_field :username %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.text_field :password %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
routes.rb:
get "signup/index"
devise_for :users
get "index/index"
root :to => 'index#index'
$ bundle exec rake routes | grep user:
new_user_session GET /users/sign_in(.:format) {:action=>"new", :controller=>"devise/sessions"}
user_session POST /users/sign_in(.:format) {:action=>"create", :controller=>"devise/sessions"}
destroy_user_session DELETE /users/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
user_password POST /users/password(.:format) {:action=>"create", :controller=>"devise/passwords"}
new_user_password GET /users/password/new(.:format) {:action=>"new", :controller=>"devise/passwords"}
edit_user_password GET /users/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
PUT /users/password(.:format) {:action=>"update", :controller=>"devise/passwords"}
cancel_user_registration GET /users/cancel(.:format) {:action=>"cancel", :controller=>"devise/registrations"}
user_registration POST /users(.:format) {:action=>"create", :controller=>"devise/registrations"}
new_user_registration GET /users/sign_up(.:format) {:action=>"new", :controller=>"devise/registrations"}
edit_user_registration GET /users/edit(.:format) {:action=>"edit", :controller=>"devise/registrations"}
PUT /users(.:format) {:action=>"update", :controller=>"devise/registrations"}
DELETE /users(.:format) {:action=>"destroy", :controller=>"devise/registrations"}
It looks like you're mixing two things: devise provides it's own signup / registration pages, and you've also created your own.
Sometimes that's appropriate, but many times the default devise pages are good enough -- or at least good enough to start with.
I'd recommend you begin by trying to implement devise with it's own pages -- leaving your signin and signup pages alone for now. You don't see the devise pages because they are hidden inside the gem.
If you want to customize them, you can get the devise pages installed in your project (in a haml format) by following the steps here:
https://github.com/plataformatec/devise/wiki/How-To:-Create-Haml-and-Slim-Views
Because you are creating you're own signup controller, I believe devise routes are not routing where you like
post your routes file and lets see if we can get your routes straightened out.
It is looking for Signup#index .. It should be looking for SignupController#index
in your routes.rb you want to have
root :to => 'signup#index'
Also, undefined method users_path is missing its route to get the users or most likely missing resources :users from your routes.rb
For a new user you want something like form_for(User.new) which will hit UsersController#new
__
Or do it this way
1)
For views/users/new.html.erb you want
<%= form_for(#user) do |f| %>
<%= render 'fields', :f => f %>
....
with a route to it something like get '/signup', :to => 'users#new' and link_to 'Signup', signup_path
2)
For UsersController add
def new
#user = User.new
..
end