Devise sign_up ignores remember_me value - devise

I'm just getting started with Devise. I have a:
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, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
end
and I've altered my sign_up view to:
<h2>Sign up</h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :email %><%= f.email_field :email %></div>
<div><%= f.label :password %><%= f.password_field :password %></div>
<div><%= f.label :password_confirmation %><%= f.password_field :password_confirmation %></div>
<div><%= f.label :contact_me %><%= f.check_box :contact_me %></div>
<div><%= f.label :remember_me %><%= f.check_box :remember_me %></div>
<div><%= f.submit "Sign up" %></div>
<% end %>
<%= render :partial => "devise/shared/links" %>
However, checking the POST response, I can see the remember_user_token set in Cookie: after sign-up whether I ticked "Remember me" or not. Anyone know why that is, and how to stop it?
By comparison, signing in only sets that cookie if the box is ticked, as expected.

I now that you are using for_for, but i use form_tag... Such code works on my project:
.form
= form_tag new_user_session_path do
= email_field_tag 'user[email]', nil, :placeholder => "Ваш e-mail", :required => true
= password_field_tag 'user[password]', nil, :placeholder => "Ваш пароль", :required => true
.submit-area
= submit_tag "Вход", :class => "orange-button"
= check_box_tag 'user[remember_me]', true
.login-link
Запомнить меня
So that part will work fine:
= check_box_tag 'user[remember_me]', true

Related

Completed 401 Unauthorized devise user login after sign up

I am working on the devise registration controller override , when i sign up with a new user ,it signs me in for the first time , but when i sign out and then try to sign in it gives me an error below in console
Processing by Devise::SessionsController#create as HTML
Parameters: {"utf8"=>"✓","authenticity_token"=>"Wq8QW/F8X1BVxFcH6M9WU8OUpIGjKI1mKd1+/OBGyGY=", "user"=> {"email"=>"jamil#gmail.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign in"}
User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`email` = 'jamil#gmail.com' LIMIT 1
**Completed 401 Unauthorized in 16ms**
Processing by Devise::SessionsController#new as HTML
Parameters: {"utf8"=>"✓" , "authenticity_token"=>"Wq8QW/F8X1BVxFcH6M9WU8OUpIGjKI1mKd1+/OBGyGY=", "user"=> {"email"=>"jamil#gmail.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign in"}
and then it gives message of invalid email or password
Please help me in this weird issue...i am hanged up
Here is my registration Form
<h2>Sign up</h2>
resource_name, :url => registration_path(resource_name)) do |f| %>
<div><%= f.label :email %><br />
<%= f.email_field :email, :autofocus => true %></div>
<div><%= f.label :Connector_code %><br />
<%= f.text_field :invitation_token,:value => #token %></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.label :Friends_code %><br />
<%= f.text_field :friend_token ,:value =>params[:invitation_token]%></div>
<div><%= f.submit "Sign up" %></div>
<% end %>
<%= render "devise/shared/links" %>
Thanks
Try explicitly setting the authentication_keys parameter either in the initializer file or the model itself:
# config/initializers/devise.rb
Devise.setup do |config|
config.authentication_keys = [ :email ]
end
or
# app/models/user.rb
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:authentication_keys => [:email]
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
end

Multiple different attachments for one model | Paperclip

I would like to have multiple different attachments for my user model, e.g. avatar and cover photo. I'm using paperclip for it.
Currently it is possible to upload a new avatar but everytime I want to update the cover photo I'm getting an error You are already signed in.. I'm using devise for authentication.
My Model:
# Include default devise modules. Others available are:
# :token_authenticatable, :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, :firstname, :lastname, :username, :login, :avatar
has_many :wishes
# Virtual attribute for authenticating by either username or email
# This is in addition to a real persisted field like 'username'
attr_accessor :login
validates_uniqueness_of :username
# hide instead of deleting
acts_as_paranoid
# tracking
# include PublicActivity::Model
# tracked owner: Proc.new{ |controller, model| controller.current_user }
# Avatar - Paperclip
has_attached_file :avatar,
:styles => {
:extra_large => "600x600#",
:large => "400x400#",
:medium => "250x250#",
:small => "145x145#",
:tiny => "45x45#",
:icon => "16x16#"
},
:default_url => '/assets/default-user-avatar/:style.jpg'
# Avatar - Paperclip
has_attached_file :cover_photo,
:styles => {
:large => "940x360#",
:extra_large => "1880x720#"
}
# Versions
has_paper_trail
searchable do
text :username, :boost => 5
text :firstname
text :lastname
text :email
end
and the form:
<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => ({ :method => :put, :multipart => true })) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :firstname %>
<%= f.input :lastname %>
<%= f.input :username, :wrapper => :prepend do %>
<span class="add-on">#</span>
<%= f.input_field :username %>
<% end %>
<%= f.input :email, :required => true, :autofocus => true %>
<%= f.input :password, :autocomplete => "off", :hint => "leave it blank if you don't want to change it", :required => false %>
<%= f.input :password_confirmation, :required => false %>
<%= f.input :current_password, :hint => "we need your current password to confirm your changes", :required => true %>
Avatar:<br/>
<%= f.file_field :avatar %><br/>
Cover Photo:<br/>
<%= f.file_field :cover_photo %><br/>
</div>
<div class="form-actions">
<%= f.button :submit, "Update" %>
</div>
<% end %>
Hope you guys can help me!
You might want to add :cover_photo to this line:
attr_accessible :email, :password, :password_confirmation, :remember_me, :firstname, :lastname, :username, :login, :avatar, :cover_photo
...and also make sure that you've got the cover_photo columns set up in your database

Rails 3 fields_for has_one not rendering form fields with devise

Hello Im using devise to register users, and I want to create a profile related to a user every time a user signs up, the problem is that when I try to add the full name of the person from the profile model on the registration view for devise's user registration, it does not show up...
This are my models:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :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, :profile_attributes
has_one :profile, dependent: :destroy
accepts_nested_attributes_for :profile
# attr_accessible :title, :body
end
This is the profile model
class Profile < ActiveRecord::Base
attr_accessible :email, :name, :phone, :code
belongs_to :user
validates_presence_of :user
end
And this is the devise view modified:
<% provide(:title, 'Sign up' ) %>
<h2>Sign up</h2>
<div class="row">
<div class="span6 offset3">
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= render 'shared/error_messages', object: resource %>
<%= f.fields_for :profile do |profile_form| %>
<%= profile_form.label :full_name %>
<%= profile_form.text_field :name %>
<% end %>
<%= f.label :email %>
<%= f.email_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
<div class="form-actions">
<%= f.submit "Sign up", class: "btn btn-large btn-primary" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
</div>
</div>
I cant see anything wrong to why the name text field would not appear...
Thank you for your help
Figured it out, it was so simple I feel bad... so here it is:
I forgot to generate a new profile on the form so it ends up looking kind of like this:
.
.
.
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= render 'shared/error_messages', object: resource %>
<div class="invisible">
<%= resource.build_profile %>
</div>
<%= f.fields_for :profile do |profile_form| %>
<%= profile_form.label :full_name %>
<%= profile_form.text_field :name %>
<% end %>
<%= f.label :email %>
<%= f.email_field :email %>
.
.
.
Hope this solves someone else's problem as well!
Thank you!
This question is already answered, but I thought I'd throw this up as an alternative for others. Form backing objects can be used to go back to a one form/one model kind of setup and help keep code cleaner.
Here's a good writeup on it:
http://pivotallabs.com/form-backing-objects-for-fun-and-profit/

Devise: Nested sign up form when the user and profile are separate models

I'm and usings devise for my authentication and would like to keep the details of my users in a separate model called profile. Profile contains information like first and last name. I want to be able to have a single sign up form that will be able to have all this information entered.
This is my form
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= render 'shared/error_messages' %>
<% fields_for :profile do |fa| %>
<%= fa.label :first_name %>
<%= fa.text_field :first_name %>
<%= fa.label :last_name %>
<%= fa.text_field :last_name %>
<% end %>
<div><%= f.label :email %><br />
<%= f.email_field :email %></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 %>
This in my user.rb model
has_one :profile
accepts_nested_attributes_for :profile
When I submit the from it doesn't save the first and last name to the database
After much searching around, this was the simplest answer to my question. Although I'm not sure this the rails way of doing things
Profile model for Devise users?

Rails 3: how to pull change password out to be its own separate form from devise

I'm using devise for my user registrations and I'd like to have a separate edit path for user's to change/update their passwords. I've pulled the devise registrations views out and created a separate registrations controller like described in Railscast 236
I've tried creating a new action called change_password in the registrations controller but when I try to set the route with match '/change_password', to => 'registrations#change_password' I get an AbstractController::Actions Not Found
registrations controller
class RegistrationsController < Devise::RegistrationsController
def create
super
session[:omniauth] = nil unless #user.new_record?
end
def destroy
resource.destroy
set_flash_message :notice, :destroyed
sign_out_and_redirect(self.resource)
end
def change_password
render_with_scope :edit
end
private
def build_resource(*args)
super
if session[:omniauth]
#user.apply_omniauth(session[:omniauth])
#user.valid?
end
end
routes.rb
match 'auth/:provider/callback' => 'authentications#create'
resources :authentications
devise_for :users, :controllers => {:registrations => 'registrations'}
resources :posts do
member do
get :likers
end
collection do
get :search
end
end
resources :relationships, :only => [:create, :destroy]
resources :appreciations, :only => [:create, :destroy]
root :to => "pages#home"
match '/contact', :to => 'pages#contact'
match '/about', :to => 'pages#about'
match '/help', :to => 'pages#help'
match '/blog', :to => 'pages#blog'
resources :users do
member do
get :following, :followers, :likes
end
resources :collections
end
end
views/registrations/change_password.html.erb
<h2>Edit <%= resource_name.to_s.humanize %></h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :validate => true,
:html => { :method => :put }, :html => {:multipart => true}) do |f| %>
<%= devise_error_messages! %>
<p><strong>To change password, otherwise leave blank.</strong></p>
<p><%= f.label :current_password %> <i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :current_password %></p>
<p><%= f.label :password, "New password" %> <br />
<%= f.password_field :password %></p>
<p><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></p>
</div><br />
<p><%= f.submit "Update" %></p>
<% end %>
Try to pass match .. in the block of devise_for helper:
devise_for :users, :controllers => {:registrations => 'registrations'} do
match '/change_password', to => 'registrations#change_password'
end
I found this to be the easiest way to split the user edit form into edit & account
Originally I had
:name,
:email,
:avatar,
:location,
:website,
:bio,
:password (all in one form)
ROUTES (this provides route:
account_user GET /users/:id/account(.:format) users#account)
resources :users do
member do
get :following, :followers, :account
end
end
USERS_CONTROLLER.RB
before_filter :signed_in_user, only: [:index, :edit, :update, :destroy, :following, :followers, :account]
before_filter :correct_user, only: [:edit, :update, :account]
def edit
#user = User.find(params[:id])
end
def account
#title = "Account"
#user = User.find(params[:id])
end
def update
if #user.update_attributes(params[:user])
flash[:success] = "Profile updated"
sign_in #user
redirect_to #user
elsif #title = "Account"
render 'account'
else
render 'edit'
end
end
(split form views)
EDIT.HTML.ERB
<%= form_for #user, :html => { :multipart => true } do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :avatar %><br />
<%= f.file_field :avatar %>
<%= f.label :location %>
<%= f.text_field :location %>
<%= f.label :website %>
<%= f.text_field :website %>
<%= f.label :bio %>
<%= f.text_area :bio, placeholder: "About yourself in 160 characters or less..." %>
<%= f.submit "Update Profile", class: "btn btn-medium btn-primary" %>
<% end %>
ACCOUNT.HTML.ERB
<%= form_for #user, :html => { :multipart => true } do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirm Password" %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Update Account", class: "btn btn-medium btn-primary" %>
<% end %>
SIDE NAV BAR INSIDE OF EDIT AND ACCOUNT VIEWS, SO THE USER HAS ACCESS TO EDIT AND ACCOUNT FORM
<ol class="nav nav-tabs nav-stacked">
<% #user ||= current_user %>
<li>
<a href="<%= edit_user_path(#user) %>">
Profile
<i class="icon-chevron-right"></i>
</a>
</li>
<li>
<a href="<%= account_user_path(#user) %>">
Account
<i class="icon-chevron-right"></i>
</a>
</li>
</ol>