Rails 3 app with Devise loading existing users - ruby-on-rails-3

We've built a new app which involves a lengthly registration process. We're using Rails 3 and devise for authentication using :confirmable.
Now we've got to import a large set of existing users from an old system. We want this to be done manually to test the process.
So the problem is we don't want to send confirmation emails to these users.
Any suggestions how we can either suppress emails and still manually complete the registration process?
Dom

Looking at the confirmable source code, it seems as if it won't send out any emails if the user is set to confirmed on create.
First off, you need to determine whether you want the newly created user to get an email or not. I'd suggest adding a checkbox to the form, or alternatively cross-referencing the email address against the old users table:
def create
# Form style
skip_email? = params[:user].delete(:skip_email)
# Old users style
skip_email? = !!OldUser.find_by_email(params[:user][:email])
#user = User.new(params[:user])
...
end
Assuming you've done one of the two, you'll have a boolean value of skip_email?. Now you can do:
def create
skip_email? = true # See above
#user = User.new(params[:user])
#user.skip_confirmation!
if #user.save
...
end
end
skip_confirmation! is a method Devise adds to the User model. You can find the Devise source code here.

Related

Assigning a random password while using Devise

Here's a breakdown of the situation -
Desired behavior: user can sign_up through Devise gem by providing their email address only. Web app generates and db stores a temporary password, unknown to user.
Logic: This is meant to be a 'gradual sign-up' process to a web application still under construction (we want to begin capturing potential users without providing access to the web app as it's still in partial development). The email is to be used for communication purposes until final release.
Problem: Devise gem requires user to input email && password during sign_up process. We've found no obvious way to circumvent the dual requirement. User failing to provide password generates error.
Potential solution: After searches and many tries, this seems to be the closest alternative (found here).
generated_password = Devise.friendly_token.first(8)
user = User.create!(:email => email, :password => generated_password)
Question: While this potential solution makes sense, we're REALLY new to this and don't understand in which file to place this code within the Devise configuration, and how to call it.
All help appreciated.
LM
OK, I kept digging until I found what I was looking for (here) - maybe it can help you too.
In your model:
before_validation :generate_password, :on => :create
def generate_password
o = [('a'..'z'), ('A'..'Z'), (0..9)].map{|i| i.to_a}.flatten
self.password = self.password_confirmation = (0..16).map{ o[rand(o.length)] }.join if self.password.blank?
end

rails devise edit other users profiles as admin using devise's forms

I'm trying to create admin roles that can go in and change other users information. I've already got everything set up pretty much except that I can't get the edit method to select the correct user to update.
Looking in the devise code, it looks like I need to update the resource to be the user of the profile that's being selected instead of the current user (which would be the admin).
How do I go about updating the resource that is sent into the devise edit view and update action? It looks like it might have something to do with this
def authenticate_scope!
send(:"authenticate_#{resource_name}!", :force => true)
self.resource = send(:"current_#{resource_name}")
end
So for example, what I want to do is
def update_resource
self.resource = User.find(params[:id])
end
Is this possible?

Show User Profile Devise Rails

I am trying to allow users profiles to be viewed. I am using devise and have followed Creating a Users show page using Devise
Currently has a route of '/users/1' with 1 being the id of the user. I would like to make it '/users/username'.
I tried to implement this by doing:
"config/routes.rb"
match '/users/:username', to: 'users#show', via: 'get'
"app/controllers/users_controller.rb"
def show
#user = User.find(params[:username])
end
Even with this ^^ the route is still 'users/1'
Use FriendlyId, it is easy to use.
https://github.com/norman/friendly_id
http://railscasts.com/episodes/314-pretty-urls-with-friendlyid?view=asciicast

Wicked Rails Gem Help Wiring Up

I want to do a multi-step form for taking in new information. One page I want to collect name/contact info, the next page I want to collect medical history, the third page demographic information.
I've installed the Wizard gem and generated a dedicated controller. All of the tutorials I've seen on it apply to devise and the signup process so I'm a little bit lost on the controller actions and the instance variables and how I should be writing them.
Was wondering if anyone has a tutorial other than a sign-up one that could maybe help me along in learning how to get this all wired up.
Any pointers or assistance is appreciated.
EDIT:
I think my problem is in the controller for my wizard.
In the show and update actions the demo shows to declare the variable of
#user = current_user
That's great, but it's a helper method that I don't need. I need to create a patient, store the patient_id in a session which I do in my create action in my main patients controller. Then somehow pass that over to the patientsteps controller.
Here's what I've tried in patientsteps
class PatientstepsController < Wicked::WizardController
before_filter :authenticate_user!
steps :medical, :summary
def show
#patient = Patient.find(params[:patient_id])
render_wizard
end
def update
#patient = Patient.find(params[:id])
#patient.attributes = params[:patient]
render_wizard #patient
end
end
When I do this, I get cannot find a patient without and ID. I understand that I'm doing this wrong, but I'm not sure how to pass in the patient_id that was created in my patients controller create action.
Patients Controller Create:
def create
#patient = Patient.new(params[:patient])
if #patient.save
session[:patient_id] = #patient.id
redirect_to patientsteps_path, notice: "Patient was successfully created."
else
render :new
end
end
In your show action, instead of params[:patient_id] you should use session[:patient_id], because the id of the patient is stored in the session, not in the params hash.
Then in the update action, you will receive the patient id in params[:patient_id], not [:id], because wicked uses params[:id] to identify which step the wizard is on.

Rails 3 saving a record issue

I have been working with Rails 3.0.5 and Ruby 1.9.2 and I have noticed that a new record doesn't get saved or isn't available for use instantly.
For example
def create
#some_record = Pool.new(params[:pool])
#some_record.users.push(current_user)
if params[:commit] == "Add More"
#some_record.save
#some_record.do_something
elsif params[:commit] == "Save"
do_something_else(params)
elsif params[:commit] == 'Cancel'
redirect_to user_url(current_user)
end
redirect_to some_other_url(current_user)
end
So when I save the record and call some_record.do_something the saved object isn't available instantly. current_user.some_records doesn't contain the newly added record but current_user.some_records.all displays the newly saved record. However on the console I am able to view the newly created record with current_user.some_records.
I'm sure I am missing something fundamental to Rails 3. I have also tried the same with current_user.some_records.build(params[:some_record] and I have the same problem. Does Rails 3 save the object instantly or is there some kind of delayed write/save because the same problem does not occur with Rails 3.0.3.
Also I am not using an authentication plugin like authlogic/devise and I simply save the current_user object to the session. I am not sure what I am doing wrong. WOuld appreciate any help?
Its also a many-to-many association between some_record and users
current_user.some_records
does not contain the newly added record because you did not save the operation after assigning #some_record.users.push(current_user).
All you have to do is to add .save after the assignment and it should work.
The model structure is not clear from your question but let's assumed that current_user belongs_to some_records
To force a database read try this:
current_user.some_records(true)
By default forcereload = false, to override this you should pass true