Unable to save user password with authlogic - ruby-on-rails-4

I am using Authlogic in rails4, and i have replaced the encrypt_password field with password.
<div class="field">
<%= f.label :password %><br>
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br>
<%= f.password_field :password_confirmation %>
</div>
class User < ActiveRecord::Base
attr_accessor :password, :password_confirmation
acts_as_authentic do |c|
#c.validate_password_field = false
end
end
The problem is i am unable to save password other fields are being saved if i'll disable the c.validate_password_field else getting Password is too short (minimum is 4 characters), that means it's not accepting the password but why ?
can anyone help ?

I got the solution.
actually i forgot to allow password in user_params.
rails4 new feature strong parameters.
def user_params
params.require(:user).permit(:username, :subdomain, :email, :password, :password_confirmation, :persistence_token, :authentication_token)
end

Related

Nested Form for Devise User

I have devise up and running and everything is work fine, I'm trying to accept_nested_attributes for the user model. I generated a model called degree:
rails g model Degree university:string course:string level:string user:references
rake db:migrate
The form is showing correct but no data is getting saved to the database? in the rails console i ran:
Degree.all
to see if any data is being saved but i get nothing?
registration_controller.rb:
class Users::RegistrationsController < Devise::RegistrationsController
before_filter :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:email, :password, :password_confirmation,
:fname, :mname, :lname, :twitterLink, :githubLink, :stackoverflowLink, :dribbleLink, :mediumLink, degree_attributes: [:university, :course, :level]) }
#devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:fname, :mname, :password, :current_password, :lname) }
end
end
Models
class Degree < ActiveRecord::Base
belongs_to :user
end
----------------------------------
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :degrees
accepts_nested_attributes_for :degrees
end
form where nested attributes are:
<h2>Sign up</h2>
<% resource.degrees.build %>
<%= form_for(resource, as: resource_name, url: 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 #minimum_password_length %>
<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="field">
<%= f.fields_for :degree do |degree_fields| %>
<%= degree_fields.label :university %>
<%= degree_fields.text_field :university %><br>
<%= degree_fields.label :course %>
<%= degree_fields.text_field :course %><br>
<%= degree_fields.label :level %>
<%= degree_fields.text_field :level %><br>
<% end %>
</div>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
degree_controller.rb
class DegreeController < ApplicationController
def new
#user = User.new
#user.degrees.build(degree_params)
end
def create
#user.degrees.build(degree_params)
end
private
def degree_params
params.require(:degree).permit(:university, :degree, :level)
end
end

Using hstore and saving new values for user

My user model contains values for things like:
name, email, phone, approved, etc..
The scope of the project I am working on includes custom user settings and for this reason I am using hstore (I dont want to create a user_settings table).
On the user edit view I want to create a checkbox called 'colors' and whether or not the checkbox is checked determines if that setting is set for the user or not.
I have already setup hstore as follows:
class AddHstore < ActiveRecord::Migration
def up
enable_extension :hstore
end
def down
disable_extension :hstore
end
end
And updated the User model as follows:
class AddSettingsToUser < ActiveRecord::Migration
def up
add_column :users, :settings, :hstore
end
def down
remove_column :users, :settings
end
end
This is essentially my user edit view:
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put, :class => "form-horizontal user" }) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name, autofocus: true %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email %>
</div>
<div class="field">
<%= f.label :phone_number %><br />
<%= f.text_field :phone_number %>
</div>
<div class="actions">
<%= f.submit 'Update', :class => 'btn btn-primary' %>
</div>
<% end %>
At this point I am unsure as to how to actually implement this functionality. The user edit page allows the user to change their name, phone and other values but how would I include a new value for the hstore settings?
Inside the form_for from where you are creating users, add your hstore fileds like below :
<%= form_for resource, ...do |f| %>
#...
<%= f.fields_for :settings do |settings| %>
<%= settings.text_field :field1 %>
<%= settings.text_field :field2 %>
<% end %>
#....
<% end %>
Then update your controller strong parameter method like below to permit these new fields.:
def user_params
params.require(:user)
.permit(
:name,
:email,
settings: [ :field1, :field2 ]
)
end
Now, you are done. Open your rails console and try some sample data like
User.create(
settings: { field1: 'data1', field2: 'data2' }
)

Can not save user sign up info when using devise and wicked

Im new to the rails platform...i have an issue of saving user sign up information on clicking register in my form....i get no error but on checking the rails console all the user information is equated to nil...cant figure out why.
my user steps controller code
class UserStepsController < ApplicationController
include Wicked::Wizard
steps :finishing_step
def show
#user = current_user
render_wizard
end
def update
#user = current_user
if #user.save
redirect_to root_path
else
render_wizard
end
end
def user_params
params.require(:user).permit(:first_name, :middle_name, :last_name, :address_first_line, :address_second_line, :city, :nationality)
end
private
def redirect_to_finish_wizard
redirect_to root_path, notice: "Thanks for signing up."
end
end
below is the code for the users controller
class UsersController < ApplicationController
def create
#user = User.new(params[:user])
if #user.save
redirect_to root_path
else
render_wizard
end
end
end
here below is the code for my simple form with user sign up information
<%= form_for User.new, url: wizard_path do |f| %>
<div><%= f.label :first_name, "First Name" %><br />
<%= f.text_field :first_name %></div>
<div><%= f.label :middle_name, "Middle Name" %><br />
<%= f.text_field :middle_name %></div>
<div><%= f.label :last_name, "Last Name" %><br />
<%= f.text_field :last_name %></div>
<div><%= f.label :phone_number, "Phone Number" %><br />
<%= f.text_field :phone_number %></div>
<div><%= f.label :date_of_birth, "Date of Birth" %><br />
<%= f.date_select :date_of_birth, start_year: 1900 %></div>
<div><%= f.label :address_first_line, "Address (first line)" %><br />
<%= f.text_field :address_first_line %></div>
<div><%= f.label :address_second_line, "Address (second line)" %><br />
<%= f.text_field :address_second_line %></div>
<div><%= f.label :city, "City" %><br />
<%= f.text_field :city %></div>
<div><%= f.label :nationality, "Nationality" %><br />
<%= country_select(:user, :nationality, {selected: "UG"}) %></div>
<div>
<%= f.label :avatar %>
<%= f.file_field :avatar %>
</div>
<div>
<%= f.label :terms_of_service, "Agree to Terms of Service" %> <br>
<%= f.check_box :terms_of_service %>
</div>
<%= f.submit "Register", class: "btn btn-primary" %>
<% end %>
for any help thanks in advance
I think there is some mistakes in your conception.
The wizard operates AFTER the sign_up of your user.
So basically, you need to create your user first without the wizard.
Creation of your User
If you're using Devise with your model User, you already have a User Controller somewhere. Actually, your class UsersController is useless if you are using Devise.
So if you want to redirect to your wizard step after the sign up, then you need to override the Devise registration controller (doc: Redirect to specific page).
To do so, create a new controller like this:
class RegistrationsController < Devise::RegistrationsController
protected
def after_sign_up_path_for(resource)
user_steps_path(:finishing_step) #add proper route
end
end
I don't have the content of your config/route.rb file but you need to have something like this :
resources :user_steps
Adding steps
Wicked wizard works like this:
- One controller with 2 actions (show and update)
- N views for N steps (1 step = 1 view, 42 steps = 42 views)
You can access the name of your current step in the actions show and update using step.
render_wizard render the view of the current step
render_wizard #user render the next step view (or the current step view if there is any error)
Using strong parameters
In your controller, you never use user_params.
So in your update action, you never the your #user with the parameters from your form. current_user doesn't call user_params for you.
Also, there is a big difference between save and update_attributes and I think you want to use the second in your update action.
It's better to place user_params in the private section also.
Final form of your controller
Your controller should look like that I guess:
class UserStepsController < ApplicationController
include Wicked::Wizard
steps :finishing_step
def show
#user = current_user
render_wizard
end
def update
#user = current_user
if #user.update_attributes(user_params)
# because you only have one step, you don't need render_wizard #user
redirect_to_finish_wizard
else
render_wizard
end
end
private
def user_params
params.require(:user).permit(:first_name, :middle_name, :last_name, :address_first_line, :address_second_line, :city, :nationality)
end
def redirect_to_finish_wizard
redirect_to root_path, notice: "Thanks for signing up."
end
end
Read the doc
I think you miss some tips in the wicked doc.
Maybe you should re-read it.
Wicked doc

Remove avatar - carrierwave and devise

I'm having an unbelievable amount of trouble getting the Carrierwave :remove_avatar checkbox to work in my Devise profile edit form. I've been following the carrierwave documentation and wiki.
I've mounted the uploader to the User model. I've added :avatar and :remove_avatar to in the ApplicationController sanitizer
application_controller.rb
def configure_permitted_parameters
devise_parameter_sanitizer.for(:account_update) do |u|
u.permit(:first_name, :last_name, :email,
:password, :password_confirmation, :avatar, :remove_avatar)
end
devise_parameter_sanitizer.for(:sign_up) do |u|
u.permit(:first_name, :last_name, :email,
:password, :password_confirmation)
end
end
I've added the :remove_avatar to the edit form
/devise/registrations/edit.html.erb
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put, multipart: true }) do |f| %>
<%= f.label :avatar %><br />
<%= image_tag(resource.avatar_url(:thumb)) %>
<%= f.file_field :avatar, accept: 'image/jpeg,image/png' %>
<label>
<%= f.check_box :remove_avatar %>
Remove avatar
</label>
<%= f.label :first_name %><br />
<%= f.text_field :first_name, autofocus: true %>
<%= f.label :last_name %><br />
<%= f.text_field :last_name %>
<%= f.label :email %><br />
<%= f.email_field :email %>
<div><%= link_to 'Cancel', resource %> <%= f.submit "Update" %></div>
<% end %>
I'm not having any other problems with carrierwave except for removing the avatars. When I check the box and submit the form the picture is not removed.
I did a test where I used the users controller instead of devise's registrations controller and submitted the form to #user instead of resource and I was able to get it working. I'd rather stick to convention and use the registrations controller. What am I doing wrong or missing?
#user.remove_avatar!
#user.remove_avatar = true
#user.save
#user.reload
Try this, May help you.
Use this in your model:
after_commit :remove_avatar!, on: :destroy

rails simple_form using virtual attributes

I am trying to incorporate Simple_forms into my app using a virtual attributes. I am following http://railscasts.com/episodes/102-auto-complete-association-revised to get autocomplete to work as well. Simple_form works when i do not use a virtual attribute but when I do use the virtual attribute I get the error "Association :head_coach_name not found".
My Team Model is:
class Team < ActiveRecord::Base
attr_accessor :head_coach_name
belongs_to :user
validates :team_name, presence: true
belongs_to :head_coach, class_name: "User", :foreign_key => "head_coach_id"
def head_coach_name
user.try(:name)
end
def head_coach_name=(name)
self.user = User.find_by_name(name) if name.present?
end
end
My User model is:
class User < ActiveRecord::Base
has_many :teams, :class_name => "::Team", dependent: :destroy
end
My View:
<%= simple_form_for #team, html: {class: 'form-horizontal' }, url: teams_path, method: 'post' do |f| %>
<%= f.error_notification %>
<%= f.hidden_field :user_id %>
<div class="col-md-6">
<div class="row">
<div class="col-md-12">
<%= f.input :team_name, class: "form-control" %>
<%= f.input :year, collection: Date.today.year-90..Date.today.year %>
<%= f.input :season, as: :select, collection:
['Fall',
'Winter',
'Spring',
'Summer'] %>
<%= f.input :season_type, as: :select, collection:
['Outdoor',
'Indoor',
'Tournament'] %>
<%= f.input :awards %>
</div>
</div>
</div>
<div class="col-md-6">
<div class="row">
<div class="col-md-12">
<%= f.input :club %>
<%= f.input :division %>
<%= f.association :head_coach_name %>
<%= f.input :assistant_coach_id %>
<%= f.input :assistant_coach_two_id %>
</div>
</div>
</div>
<%= f.button :submit, label: "Add team", class: "btn btn-large btn-primary col-md-3 pull-right" %>
<% end %>
Everything works as long as i don't have the virtual association in there. I could put :head_coach and it would work with a drop down list but I want to use the autocomplete feature like in the railscast video. Also in rails console i can run these commands to show the virtual attribute works:
2.1.2 :003 > team.head_coach_name
User Load (0.6ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 100 LIMIT 1
=> "Miss Daisha Shanahan"
Any ideas on how I can get the virtual attribute to work correctly with simple_forms?
You are referencing an association that doesn't exist with f.association :head_coach_name. Try this instead:
f.association :head_coach, label_method: :head_coach_name
You have a few other oddities in your code, including your head_coach_name definition, which should rely on head_coach instead of user. Same concern for the head_coach_name= method.
Example. You have:
def head_coach_name
user.try(:name)
end
Seems like you should have:
def head_coach_name
head_coach.try(:name)
end