Adding additional fields (with validation) to Devise view/model in Rails app - ruby-on-rails

It seems there's not a whole lot of documentation out there covering how to add custom fields to a Devise authentication solution in a Rails 3 app.
Along with the required e-mail and password for users to sign up, I'd like a few other custom fields put in and validated and since there are no controllers generated with Devise, how does one do this?

I needed this same thing. There is a great article I found to help me:
http://ykyuen.wordpress.com/2011/03/03/rails-%E2%80%93-add-custom-fields-to-devise-user-model/

When you do rails generate devise_views all the views for the features you selected will go in your app/views/devise folder. You could simply add the fields defined in your model to those views. If you need to customize the controller, create
class YourModel::DeviseFeaturesController
by DeviseFeatures I meant RegistrationsController, ConfirmationsController or whichever features you decided to use.
There is a great screencast about it: RailsCasts-customizing-devise

Related

Customizing Devise vs Creating own authentication for multi page registration

I'm creating a ruby on rails project (soon to be a mobile app). I wanted to utilize the Devise gem for authentication, but I am struggling with the customization of Devise. I wanted to implement a multi-page registration/sign up process. The standard devise gem automatically provides a form that includes email and password. For my project, I wanted the users to add in their name on one page, then their email on the next page, and then their password on the page after, etc. Please see the attached jpg picture for a super basic example.
So far, I separated the registration controller from the devise package. The registration controller comes with the new.html.erb and the edit.html.erb files. I want to take the standard form in the new.html.erb file and split that into multiple html.erb files. The controller would obviously need to be connected to these multiple files. As I was working on it, I realized how complicated it is. I'm wondering is it better to create my own authentication process instead of devise, or is it better to stick with devise?
I really want to make it work with devise, so if you have any suggestions on how to customize the gem to implement the multipage registration that I want, please let me know.
This can be done by modifying the views and the devise controller to accept multi-part registration, but it is cumbersome: I would counsel you to avoid it. Rather, another option would be to: (i) use devise to handle the email and username in one form and then (ii) handle the multi-part registration using the wicked gem - whose sole purpose for existence is to handle multi part registration problems.

Ruby on Rails: Devise - Is it bad to have a Users Controller separate from devise?

So from the beginning of the project, I installed devise gem, did the migrations and everything. Would it be bad practice, if I created a new controller:
rails g controller Users
Along side with devise? Sorry for the n00b question. Is there like a secrete place that devise creates this controller already and I can just customize and modify?
I think that it depends what you're trying to accomplish. If you want to customize Devise, Devise provides some hooks that you can use to customize certain things such as after_sign_up_path etc, or you can subclass Devise built-in controllers, for example:
class MyRegistrationsController < Devise::RegistrationsController
end
If you want something that devise doesn't provide, eg a list of users, or a detail page for a user, you might want to just create your own users controller as you mentioned - not bad practice, and Devise doesn't have any secrets, you can poke around in the gem code on Devise to find out what it's providing and what you might want to add or customize.

Rails 3.x authentication using existing table fields

I have Rails 3.2.11 application that i need to hook up with login. The Devise Database Authenticatable would have been ok except:
I can't create table/fields and need to use existing fields(Devise wants to create user model).
Instead of user model, i have to use "existing model (student) with email and password fields.
Would any of you Rails guru tell me how to customize Devise or if to use something different.
Basic requirement is: use login system where someone has to register but use existing table/fields.
Thank you
The quick answer if you can't add fields to your table, Devise is not a authentication choice. It needs certain basic specific fields to work.
You can set Devise with a specific model name, as Peter de Ridder points out. But, without these required fields, several wild errors will show up (like "missing column" among others more cryptic).
Note that in this Devise's wiki article refers to these fields as required:
https://github.com/plataformatec/devise/wiki/How-To:-change-an-already-existing-table-to-add-devise-required-columns
You can customize devise as much as you want. Railscasts #210 gives information about customization options. You can create a devise model with any name you want btw. For example, you can do:
rails generate devise Student
If you want a Student model. Pretty much everything in Devise can be customized, altough some changes are easier then others. You could also get all the controllers from devise at github and customize or just override them in your own application. The devise wiki has a lot of information about customizing:
https://github.com/plataformatec/devise/wiki
I can also recommend the revised railscast #250 authentication from scratch (also railscasts available for authorization from scratch), if you want full control on all your authentication options.

How Do I Create a User Profile With Devise?

I really like how devise offers an easy to use registration system out of the box but I'm having trouble extending it to do what I need. I need to create a public user profile for each user that shows their information like name, email, bio, and more info. I've done this in the past before with a users/show function but since devise doesn't provide any easily editable controllers, I'm having trouble figuring out how to do this. I've already run rails generate devise:views to copy the devise views to my app but I don't know where to go from here. Any help would be much appreciated.
Sounds like you want users to update their profile at the same time they create their account? If so, you can setup an associated Profile model with the User model. Using accepts_nested_attributes_for you can then create a record for the nested model on devise user registration submit/creation.
Here's a great screencast covering nested models and I also suggest you search other devise relate SO posts as this question has been discussed before.
There is an alternative approach, that is simpler to implement — only allow registered users edit/update their profile. This way you don't have to alter the Devise views and you can setup the various CRUD actions via a separate non-devise controller.
Throw in an Access Control List (ACL) solution such as CanCan (there are other alternatives too!) and you can even allow other users view profiles but deny access to edit/destroy etc.

Sign up — Two models, One form

I'm working with Rails 3.1, Devise and Mongoid. Right now I have 3 models — User, Client < User and Developer < User. Is there a way to sign up as either client or a developer from one form using say radio for checking desired account type?
It seems like I can edit only devise's views but not make some changes to controller. Or now?
In Ruby (then in Rails), you can redefine whatever you want, whenever you need.
So indeed, you can redefine the controller if you want to and override one or several actions.
Just create the controller file at the same level it's in the gem itself, or make good use of class_eval.
Using Devise, make sure you add your additional attributes in the attr_accessible list.

Resources