User CRUD in web application that handles registration and login with Devise - ruby-on-rails

I am currently working on application that is build on rails 3.0.9. All of a sudden the client decided that there should be a place to create a user and edit a user in the admin section of the website.
Now here is the case. The authentication and registration in the web application is handled by devise. If I try to implement a custom USER create method in my controller how should I hash the password in the very same way devise is doing so that I can store that in the database. This also applies to editing the already registered users as well.
I have tried to find the solution but no use. Any help in resolving this would be appreciated.

That's easy. You can setup another controller and form but set it up on your User model. Your form will need to include :email, :password, and :password_confirmation. When you do #user.save in your controller's create action, this will have devise take care of all the hashing requirements under the hood.
If you want to check that your save works (just for testing), add a bang at the end like #user.save! - this is only for testing. Either drop into rails console and you can see the newly added records or tail your log file.
Editing should work along the same lines and you can do #user.update_attribute() in your edit action, or #user.update_attributes() if you prefer mass assignment (this will be subject to any attr_accessible restrictions in your model)

Related

Rails 4 Devise - Check if username or email exist before submit

I am currently taking some time to study Devise with Rails 4 for a future app I am planning to create. I have successfully managed to allow users to access to the application via their username or their email.
Now, I would like to inform users whether the username they've picked has already picked up by an other existing user during the signup process and before the user hit the submit button. Github homepage has a three input field form that illustrates my aim.
For that I added to app/models/User.rb the following code
validates_uniqueness_of :username, :email
But this seems to not be sufficient as it only informs the user once the form has been submitted. I imagine that I could try to do this via JS but I am wondering whether there is Ruby only way to achieving this.
Disclaimer: I am fairly intermediate to Rails and a total beginner to Devise.
Thanks for your answers.
There is no ruby only way to do this, you have to use javascript (ajax) to do it, and Rails give you tools to distribute the js to the page and communicate with it.
You have to remember that all the ruby code is executed on the server. The browser only work with the rendered HTML/Css and javascript.

Devise: is it possible to de-couple registerable and the user edit profile functionality?

I want to disable user sign up, but still provide edit profile functionality for existing users. Is this possible?
Currently, removing :registerable from th options list also disables the edit profile functionality and edit_user_registration_path is no longer defined.
Any way around this? It is strange that seemingly unrelated functionality is coupled this way.
What I would do would be to create a registration controller and use devise mappings to use that new registration controller. Then for the new and create actions set a flash message and redirect to the root of the app (or your chosen location). If you want to lock it down even more, just override the create method on your user model and throw an exception.
See this answer- disabling Devise registration for production environment only

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.

Using devise helpers in model

Is there any way to use devise's controller helpers in model namely user_signed_in? I have tried adding the following line to my user model, but that doesn't seem to work:
include Devise::Controllers::Helpers
More specifically, I want users to be allowed to be created without password, for which I am implementing the method 'password_required?'. In that method I want to check (before creating the user) if another user is creating that user, or weather he/she is signing up. Any help would be much appreciated.
You cant access controller helper within the model. however you can build an association between users that would allow you to create users on behalf of one another
take a look at rbates screencast on how to implement it
http://railscasts.com/episodes/163-self-referential-association

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.

Resources