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.
Related
I have been developing a rails app in which we allow different users to submit different values for an attribute of a model.
For example all users can submit values greater than 10 while administrators are allowed to submit one-digit values as well.
As we know models do not have access to the current_user helper, therefore you can not write a custom validator with ease.
What's the best practice when implementing such a feature?
This is a perfect use case for validation contexts. You define the contexts in your model, but then your controller specifies which context to use.
Here is a nice article by Justin Weiss about them—although he doesn't mention that they solve problems not solved well any other way, per-user validation rules being the perfect example, since your model doesn't have access to the current user, and your controller isn't designed to specify validation rules:
https://www.justinweiss.com/articles/a-lightweight-way-to-handle-different-validation-situations/
It's always advised to keep logic like current_user outside your model.
But given your requirement,
Well, as our controller knows current_user then it should be the controller that tells your model about it.
So in your model add an attr_accessor for current_user
and in your controller do something like
#model_obj.current_user = current_user
Now all this being said. I'd propose using an alternative to model validation. Because in real what you are trying to do is giving users permissions based on their role. You'd be better of using a gem like Pundit for it.
This is probably a case where you enforce the validation in the controller. Strictly speaking this is not about model validation, but about user authorisation. Pundit is a nice gem for authorisation or you can roll your own.
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.
I am building a sample app for learning rails 4, and I'm a little confused on where I'm meant to build certain things. For example, I want to check if a user is logged in, and if so, display their account balance in the header (a partial).
Thanks to Michael Hartl's tutorial, I have a function to check a user's login status in the session helper, which is included in the application controller and can therefore be accessed in the partial.
Since the balance is tracked in the Users table, do I build a function get_balance in the Users model? Or should I create a function in the application helper? If I do build it in the application helper, is this auto-included in the application controller, or do I have to include it specifically? If I don't build the function in the model, can I still access the User object?
Thanks for your patience with a noob.
Since your users balance is a column in Users table, it is already there for you as a field (most possibly user.balance). And yes, this is where you should store it. You might use helpers for stuff that is related to general layout of your application and use combination of partial view and layout to spread it around.
Since it's already on your table, assuming your user is logged in, you could just call
current_user.balance
But it sounds like you want to add onto the data given,
I would suggest perhaps using a Rails decorator for your user model.
Basically a decorator adds an object-oriented layer of presentation logic to your Rails application.
I use the Draper Gem
I am about to build a new site in ruby on rails for residents at my college.
The site will allow residents to change their passwords for the college-firewalls (which means there are certain requirements).
On the site, each resident will have an account with a number of data assigned to it, and for this I need some authentication.
I've been studying Devise for almost the entire day now, but im starting to wonder if I have a too complicated task, to complete it with Devise.
Problem is, I need the passwords to be stored with DES-encryption, something Im not sure if Devise can handle.
Another thing is, users can't make their own profile. Admins will do that (to ensure correct data), which means that user-creation is not the default one. Since there are no controllers for this, is it even possible to do it that way?
I'm not sure if I should keep on going with Devise, or bite the bullet and write it all from scratch instead. Some opinions would be appreciated.
This page on the Devise wiki ( https://github.com/plataformatec/devise/wiki/How-To:-Create-a-custom-encryptor ) tells you how to set up a custom encryptor.
To make it so that admins create a user, remove the :registerable module from the User model. Then add a user resource to your app, example:
scope 'admin' do
resources :users
end
Set up the new/edit pages with your profile fields, etc., normal rails programming.
For an example using CanCan to control access to the users resource, have a look at this post: http://zyphmartin.com/blog/manage-users-with-devise-and-cancan.
If devise does not exactly do what you need, maybe this recent webcast from Ryan Bates will help you.
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