I would like to update the "User" model which has a column named "verification_code_confirmation" from the "Verifications" Controller
I had tried writing this code
class VerificationController < ApplicationController
def index
#user = User.find(params[:id])
if #user.update_attributes(params[:user])
redirect_to #user, :flash => { :success => "Successfully verified." }
else
render :action => 'edit'
end
end
and
index.html.erb for verifications view.
<%= form_for(#user) do |f| %>
<div><%= f.label :verification_code_confirmation %><br />
<%= f.number_field :verification_code_confirmation %></div>
<% end %>
User model
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :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, :verification_code_confirmation, :phone
end
end
I have updated to
if #user.update_attributes(params[:user])
How do i do it? I have looked around in the internet to update my column through the verification controller but i couldn't.
Related
When I am logged in as an admin on my rails app, I go to the admin page and then to the Users column in activeadmin (Which includes admin users). When I click edit for the admin-user that is currently logged in, I edit the fields, and then when I click Update User I get the following error:
ActiveRecord::RecordNotFound in Admin::UsersController#update
Couldn't find User with id=5 [WHERE ('t'='f')]
This only happens when I do the above procedure for the current_user and not for any other user.
This my admin/users.rb file:
ActiveAdmin.register User do
index do
column :email
column :current_sign_in_at
column :last_sign_in_at
column :sign_in_count
default_actions
end
filter :email
form do |f|
f.inputs "Admin Details" do
f.input :email
f.input :initials
f.input :password
f.input :password_confirmation
end
f.actions
end
end
And Here is my user.rb model file:
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
has_associated_audits
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me ,:name, :initials
has_many :entries
end
[WHERE ('t'='f')] Condition can never be true !
I think I have a routing issue with Devise that I can't figure out. I am trying to implement this method for having users sign up using Devise for authentication. The example uses haml, and I have attempted to translate that back to erb, hopefully with some success.
Rails 3.2.8 in development on Mac OS X, using PostgresQL
Using Devise and simple_form
app/views/devise/registrations/new.html.erb:
<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%# simple_form_for([:backend, #user]) do |f| %>
<%= f.error_notification %>
<div class="inputs">
<%= f.input :email, :required => true, :autofocus => true, :label => "Email" %>
</div>
<div class="actions">
<%= f.button :submit, "Add User" %>
</div>
<% end %>
app/views/devise/confirmations/show.html.erb:
<%= simple_form_for(resource, :as => resource_name, :url => confirmation_path(resource_name)) do |f| %>
<div class="inputs">
<%= f.input :password, :required => true, :label => "Password" %>
<%= f.input :password_confirmation, :required => true, :label => "Confirm Password" %>
<%= f.hidden_field :confirmation_token %>
</div>
<div class="actions">
<%= f.button :submit, "Confirm" %>
</div>
<% end %>
app/modesl/user.rb:
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, :confirmable,
:timeoutable
# Setup accessible (or protected) attributes for your model
attr_accessible :email,
:password,
:password_confirmation,
:remember_me,
:username
def password_required?
super if confirmed?
end
def password_match?
self.errors[:password] << "can't be blank" if password.blank?
self.errors[:password_confirmation] << "can't be blank" if password_confirmation.blank?
self.errors[:password_confirmation] << "does not match password" if password != password_confirmation
password == password_confirmation && !password.blank?
end
end
app/controllers/confirmations_controller.rb:
class ConfirmationsController < Devise::ConfirmationsController
def show
self.resource = resource_class.find_by_confirmation_token(params[:confirmation_token])
super if resource.confirmed?
end
def confirm
self.resource = resource_class.find_by_confirmation_token(params[resource_name][:confirmation_token])
if resource.update_attributes(params[resource_name].except(:confirmation_token)) && resource.password_match?
self.resource = resource_class.confirm_by_token(params[resource_name][:confirmation_token])
set_flash_message :notice, :confirmed
sign_in_and_redirect(resource_name, resource)
else
render :action => "show"
end
end
end
app/config/routes.rb:
devise_for :users, :controllers => {:confirmations => 'confirmations'}
devise_scope :user do
put "/confirm" => "confirmations#confirm"
end
resources :users
When I input my email address at the users/sign_up page, everything works fine. I receive the confirmation email, click the confirmation link and am directed to:
/users/confirmation?confirmation_token=[CONFIRMATION TOKEN HERE]
Then, when I fill in my password and submit, I am directed to this page:
users/confirmation
with the following error:
ActiveRecord::RecordNotFound in UsersController#update
Couldn't find User with id=confirmation
I'm pretty sure it's a routing issue. Any ideas? Thanks so much!
Cyle, thanks for getting my brain working this evening. The answer was staring me right in the face the whole time. In routes, I changed
match "/confirm" => "confirmations#confirm"
to
match "/confirmation" => "confirmations#confirm"
Now I can start researching the next error it throws. Thanks again!
Rails 3.0
Following these instructions:
https://github.com/plataformatec/devise/wiki/How-To%3a-Require-admin-to-activate-account-before-sign_in
I've generated a migration :approved (boolean) for my devise user.rb. Now I want to edit it with a checkbox from a different controller: unapproved_users_controller.rb.
When I load the form in the edit I get this error: undefined method `user_path'.
routes.rb, the resources for my new controller
resources :unapproved_users
app/models/user.rb, notice that :approved is attr_accessible.
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me, :approved
def active_for_authentication?
super && approved?
end
def inactive_message
if !approved?
:not_approved
else
super # Use whatever other message
end
end
def self.send_reset_password_instructions(attributes={})
recoverable = find_or_initialize_with_errors(reset_password_keys, attributes, :not_found)
if !recoverable.approved?
recoverable.errors[:base] << I18n.t("devise.failure.not_approved")
elsif recoverable.persisted?
recoverable.send_reset_password_instructions
end
recoverable
end
end
app/controllers/unapproved_controllers.rb
class UnapprovedUsersController < ApplicationController
def index
if params[:approved] == "false"
#users = User.find_all_by_approved(false)
else
#users = User.all
end
end
def edit
#user = User.find(params[:id])
end
def update
#user = User.find(params[:id])
#user.update_attributes(params[:user])
end
end
app/views/unapproved_users/index.html.haml
%h1 Users
= link_to "All Users", :action => "index"
|
= link_to "Users awaiting approval", :action => "index", :approved => "false"
%table
- #users.each do |user|
%tr
%td= user.email
%td= user.approved
%td= link_to "Edit", edit_unapproved_user_path(user)
app/views/unapproved_users/edit.html.haml
= render 'form'
app/views/unapproved_users/_form.html.haml
= form_for (#user) do |f|
-if #user.errors.any?
#error_explanation
%h2= "#{pluralize(#user.errors.count, "error")} prohibited this user from being saved:"
%ul
- #user.errors.full_messages.each do |msg|
%li= msg
.field
= f.label :approved, 'Approved?'
= f.check_box :approved
.actions
= f.submit 'Save'
You need to change the form_for.
It should be
= form_for(#user, :url => unapproved_user_path(#user)) do |f|
I've been stuck on this for over 24hrs trying to follow other solutions posted here, but I can't get this to work. I'm new to Rails and need help!
I want to get my /users/edit page working so that I can simply change a users password. Originally, I wanted to do it without current_password but I don't mind leaving it in there as long as I can get the password changing and updating.
Here's what I did:
I followed the example in the Devise Wiki and inserted it into my Users controller which I specified to inherit from Devise::RegistrationsController
class UsersController < Devise::RegistrationsController
...
end
I changed my routes:
devise_for :users, :controllers => { :registrations => 'users' } do
match '/users' => 'users#index'
end
And here's my model:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessor :password, :password_confirmation, :current_password
attr_accessible :email, :password, :password_confirmation, :current_password, :remember_me, :full_name, :coach, :bio
validates :full_name, presence: true
end
I was assuming the UsersController I created would override the Registrations controller and that I would be able to change/update password. It works to the extent the redirect to root_path happens (which is only meant to happen after updating without current password) but the new password is not saved (I checked the logs and there was no SQL to show it was saved)...
Any ideas?
Try doing something similar to:
Devise Forgot Password for logged in user
This lets you have a separate view for changing the password.
The key is that I couldn't ever get it working in devise, so I wrote my own solution in the users controller and post to that instead of using the methods provided by devise.
add this to your user form where you want to be able to edit the password:
<%= form_for(#user, :url => url_for(:action => :do_reset_password) , :html => { :method => :post }) do |f| %>
<%= f.hidden_field :reset_password_token %>
<div><%= f.label :password, "New password" %><br />
<%= f.password_field :password %></div>
<div><%= f.label :password_confirmation, "Confirm new password" %><br />
<%= f.password_field :password_confirmation %></div>
<div><%= f.submit "Change my password" %></div>
<% end %>
users controller:
def do_reset_password
id = params[:id]
# there may be a better way of doing this, devise should be able to give us these messages
if params[:user][:password] != params[:user][:password_confirmation]
flash[:alert] = "Passwords must match."
redirect_to :back
return
end
if #user.reset_password!(params[:user][:password],params[:user][:password_confirmation])
#user.save
respond_to do |format|
format.html { redirect_to '/home', notice: 'Your password has been changed.' }
end
else
flash[:alert] = "Invalid password, must be at least 6 charactors."
redirect_to :back
end
end
config/routes.rb
resource :users do
post 'do_reset_password'
end
I want to add the current_user.id from devise to another model...
question model:
class Question < ActiveRecord::Base
belongs_to :user
attr_accessible :content
validates :content, :presence => true
end
user model:
class User < ActiveRecord::Base
has_many :questions
# 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
end
my question migration:
class CreateQuestions < ActiveRecord::Migration
def change
create_table :questions do |t|
t.string :content
t.date :deadline
t.integer :user_id
t.timestamps
end
end
end
In my questions controller:
def create
params[:question][:user_id]=current_user.id
#question = Question.new(params[:question])
if #question.save
redirect_to questions_path, :notice => "Successfully created question."
else
setup_questions
render :index
end
end
When i save the question the current_user.id won't be recorded in my database?
Try this instead:
def create
#question = Question.new(params[:question])
#question.user_id = current_user.id
if #question.save
redirect_to questions_path, :notice => "Successfully created question."
else
setup_questions
render :index
end
end