Building a user register page - ruby-on-rails

I am trying to setup user registration. I have been recommended not to use scaffold.
So far I have run this command to generate model for all profile information:
rails generate model User name email birthday:date zip_code time_zone sexuality career education religion politics children height does_user_smoke does_user_drink about_me:text career
I want the required sign up information from when a user first visits the website to be name, email, birthday, zip code, and password. I am not sure what the next step is. As far as building the page to have the users sign up. I know I have to edit app/views/users/new.html.erb. When I did the tutorials I ran scaffold which did the work for me, but now I have been told by several people to not user scaffold and just do everything yourself. But I haven't read a tutorial, nor can I find any examples on the internet that will walk me through building a user register page in rails.
So I don't know if I should go back to using scaffold or hopefully someone can point me in the right direction with starting on the user register page.

Yes, there are tutorials about authentication. I personally like Ryan Bates's screencast authentication from scratch. That tutorial should include everything you need.
As for the scaffolding, I think it really depends on what you're trying to achieve here. All scaffolding does is create the basic CRUD (create, read, update, delete) operations on a model for you, including controller, views and tests. Since you needed those views, for example, you could have used that. And prewritten tests are awesome.
However, in this case I would just go with the screencast and not use scaffolding. Scaffolding is nice for any model that doesn't need anything fancy like authentication, but only the basic CRUD operations.

Try to Analise that do you want views/controllers/models or all.
Since this problem is specific to user registration, so you may only need to have user registration view and user model and the controller that performs the registration logic.
Scaffolding is useful to automate your task specifically if you want a complete MVC functionality.
For ex:
If you use the controller generator, this will generate the controller action and the view.
rails g controller controllername new create
This will create new and create actions with their relevant views.

Related

Admin Create/Invite New User with Devise

I have a simple rails app using devise--as such (and being a new rails guy), some of these methods are super classed and inherited, I think...
How can I create a few methods in my brand new admin controller to 1) Make a new user and 2) edit current users?
I feel that the typical CRUD operations will make a lot of redundant code and I would like to use some rails best practices to simply allow admins to edit multiple profiles (where users can only edit their own) and also add...
Again, I'm new to rails, so speak slowly. :)
The devise wiki has everything you need to configure/customise devise https://github.com/plataformatec/devise, I know that's not the exact answer but its a good place to start.
have you looked at active admin? http://activeadmin.info/ Works well with devise and gets you up and running with an administration framework very quickly

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 simple website layout

I'm learning RoR, I've read some tutorials (railstutorial for the first one),
but I've a problem to define the logic layout of the my first simple website.
The structure is:
When you go to mysite.com you see a welcome page with the signup form or the link for login.
If you signup or you login into the site, you are at mysite.com/dashboard and you see a list of your messages.
You can go to mysite.com/$username and you see a page with a form where you can write a message for the $username.
Stop. That's it. It's very simple, I know, but is for learning.
The problem is this: I'm new to MVC paradigm and I don't know how structure the logic layout of my app. Of course there'll two models: User and Message. But for controllers? And which functions in any controllers? Should I use scaffolding?
Please give me a help, I'm very confused.
Thank you.
Controllers are the logic for the data, so to login/sign-up is really validating/creating a user, if you need to view the users dash board, well that's a look up on the user data so he goes there as well.
The messages, that will be a separate controller that can create/view messages!
As others have pointed out, your controllers contain the logic for your code and invoke views based on that logic by rendering or redirecting to pages. You can define whatever actions you want in your controllers, and then use routes to map a particular URL to a controller action. That being said, Rails gets a lot easier if you "go with the flow" and make some simple assumptions about the actions that could happen. Both your users and your messages represent rows in their respective database tables. There's no much you can do to a row in a database table - you can Create it, Read it, Update it, or Delete it (CRUD). If you define your actions in terms of these four logical actions, Rails lets you generate some easy routes.
You can back into any URL schema that you want, but what you are describing is:
Read the messages that are for a user on the dashboard
Create a message for a user when you go to another page (mysite/username)
Each of these maps to a CRUD action that you should be defining in your controllers.
Agreed also with other advice to simply do a few more tutorials that will probably clear this up.
If you haven't already, read Getting Started with Rails. Look out for the discussion on MVC and scaffolding. Playing around with scaffolding can help you learn where things go and is a great place to start for beginners.
Also, I highly recommend this book: Agile Web Development with Rails. It is very hands on and an easy read.

Authentication with Ruby on Rails & Devise

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.

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.

Resources