Using devise helpers in model - ruby-on-rails

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

Related

Add specific user roles to a specific model instance using CanCan and Rolify

As I work may way through a Rails app, I have another question. The app allows for users to sign up via Devise. That works. Within the app, users are able to create their own groups called Circles. That works. Now I want the user to be able to add other users to their Circle. This doesn't work.
So far I've implemented CanCan and Rolify. I'm able to assign a role to a user from the User model, so I know the setup is working just fine. What I cannot figure out is how a user can add another user to a specific Circle. I've looked over SO and found something close to what I want to do here: Authorization in Rails 3.1 : CanCan, CanTango, declarative_authorization?, but this doesn't work for my situation.
I know how to add a role at the application level with CanCan. What I want to do is add a role at the specific Circle instance. Not all users will have access to a Circle, only those who are added.
I'm open to other ways to accomplish this if anyone has any ideas. What would be the best way to set this up?
EDIT
I figured out how to do this from rails console. user.add_role :moderator, Circle.find(22) This works perfectly and saves to the database. How can I do this same thing through a form in a view?
Use a controller....it will add the role after that you submit the form! In the controller you can just use a class method for example:)
Let me know if I need to be more explicit!
Cheers

Creating new users through Devise with an admin user who is already logged in

I'm building a service on Rails using Devise which requires an 'admin' user to add regular users to their organization account.
The default behaviour of Devise doesn't support this, as the ':require_no_authentication' method is called when a logged in admin user tries to create a regular user account.
What would be the recommended method of achieving the functionality I am looking for?
:require_no_authentication is called by prepend_before_filter in the
Devise::RegistrationsController class, rather that in one of the
RegistrationsController methods, so I do not know if this can be
overridden (correct me if I'm wrong).
I believe separating the admin users from the regular users would
work, however these users will share very similar properties, so I
believe doing this will add unnecessary repetition.
I am currently trying to create new admin users (who in turn create
the organization that regular users belong to) using the regular
Devise sign up flow with 'users#new' and 'users#create' controller
actions, and allowing admins to add new users through a 'users#add'
action.
If there is perhaps another good user authentication gem that would better suit my needs, I would be happy to take a look at switching to that.
This seems to be more of an authorization problem than an authentication problem. You can use an authorization gem, such as cancan, to assign roles to users (such as admin) and grant abilities to those roles. This works really well alongside Devise. Here's a tutorial:
http://www.tonyamoyal.com/2010/07/28/rails-authentication-with-devise-and-cancan-customizing-devise-controllers/
EDIT: I think I may have misunderstood your problem. Maybe what you need is just another controller to handle the creating of users outside of the Devise controllers. You could use cancan to restrict access to this controller to only admins.

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.

Ruby on rails, devise, how to set up a user profile

I'm new to rails, and even more new to devise.
I have set up a user controller and I've managed to create a view showing the e-mail address of the current user. Now I want a user profile page, where the user can add personal information etc, and keep track of published posts etc. Basic community functions.
How should I design this? Should I create another controller and model, like userprofile? I already have a controller called users, created by devise. Can I reuse that or should I create a new one?
I would love some examples in order to get the idea. I'm still a little confused with the concepts used of rails.
Your controller should be named UsersController, just to be sure. The model class will be named User, and the table on the database users.
If you do the normal creation of a RESTfull controller, it will have the actions index, new, show, and delete. That maps pretty well to what a user will want to do. The only thing you have to think about is if all users should have the right to see all information stored in the profile for a user, but that won't change if your model will be userprofile.

How to create an object within involve db in RoR?

I know that I can use the db schema to easily generate CRUD for me. But I want to do a simple login page, it only using the "user" object, check whether the password & user name is correct. So, I want to create an "system" object which have a "login" method. How can I do that in RoR?
You can create a Controller and views without model and that will work. It looks like you want a super simple login which is described in this railscast. To see how to restrict access to pages, see the previous railscast

Resources