Devise, how to create a edit profile page - ruby-on-rails

I have a Rails 3 App using devise. I want to create a User/Profile edit page. Where a user can edit the name, add/change their photo etc...
I'd like to do this in the right rails/devise way. What's the way of handling this? I see a app/views/devise/registrations/edit.html.erb file
Do I edit that file?
Or do I create a app/views/users/edit.html.erb and customize that experience? But then what do you do regarding the controller? Create a new controller?
Thanks

You may edit the devise/registrations/edit.html.{erb,haml} file and customize it to your needs, maybe even add any additional fields that may be in your user model but not on the form.
I believe it's also possible to have a common CRUD interface for users along with the Devise's, but then you'd have to create a new controller and add the views and everything, so it is easier and preferable to simply override Devise's views to change or add what you need.
You can generate them with rails g devise:views.

You could also create a user profile edit page directly from a controller of your choice in the app that modifies your user model appropriately.

Related

Best practice when developing an RoR application

I am currently creating an application. It's relatively simple, lets say for now it only contains users & posts
What I am trying to do is display basically a blog with a new post form at the top in case the signed in user is admin.
I have two options:
I can use an index for posts and keep it all within the posts controller. I could add a form to the index.html.erb which checks for admin attributes.
However, I will most likely use the index functionality later on in other parts of the app.
Second option would be to create a static page called blog and render the form view and all posts.
Both should be possible, but what is the "rails way"? Or is there no best practice?
Controllers should be RESTful, and should be appropriately named for the resources they manipulate.
The index action of your PostsController should have one purpose ... providing appropriate information relating to all of the posts to its view. The exact output of this could change within the view depending on whether you're logged in as an admin or not, but essentially the role of that action should be restricted to that function.
I would advise you to take a look at the CanCan gem and think about how you could use that to authenticate users, providing appropriate page content to admins and normal users alike.

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

Rails post create is redirecting to wrong controller

I've created a category controller in the admin namespace, and have another category controller for the actions which won't modify a category. I'm doing this because I need the admin index and show actions to show drastically different templates in the administrative section of the site compared to the front-facing views. However, Rails by default routes from admin categories new, to non-admin categories create. How can I make new and edit call create and update respectively in the admin categories controller? If anyone has suggestions for a better controller layout, I'd be grateful for some insight to good design practices as well.
you can use routes namespaces. It will help you to keep admin's logic isolated
Unfortunately my problem was very trivial. I should have double checked the URL that my Rails program was loading. I hadn't changed my site's admin page to redirect to admin_categories_path, simply categories_controller. Changing this fixed my issue.

Adding additional fields (with validation) to Devise view/model in Rails app

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

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