Best way to create a login and signup that is DRY

Hello! I am learning RoR and have started a small project to help me learn it, I have a question, as Rails tries its best to be DRY… I have a User model in the database that looks like this:

create_table "users", force: :cascade do |t|
    t.string "first_name"
    t.string "last_name"
    t.string "phone_number"
    t.integer "tokens"
    t.string "relation"
    t.boolean "verified"
    t.boolean "deleted"
    t.integer "available_loan"
    t.string "email"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.boolean "admin"
    t.string "password"
  end

I want to create 3 pages that uses this model, login, that only requires the users email and password, signup, which will require first_name, last_name, email, password, phone_number and relation, and finally an edit user page that needs all that. The most important here is the login and signup, in my routes I have:

get “/signup”, to: “users#new”

post “/signup”, to: “users#create”

Which are to sign up, now how do I separate login and signup here? I assume in the .html.erb file I will need to check the page route, and if it is login to only display some of the form, and if signup to display more of it? This feels wrong… Is it?

Also! Any tips and tricks and advice you can give me as a newbie to Ruby would be awesome!

Thank you in advance!

I assume in the .html.erb file I will need to check the page route, and if it is login to only display some of the form, and if signup to display more of it? This feels wrong… Is it?

I would have a separate controller/action for the login. Probably call it SessionsController with new presenting the login form and create actually establishing the session. In my routes you can have:

get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'

This way your login process and signup process are each their own code, templates, etc.

Any tips and tricks and advice you can give me as a newbie to Ruby would be awesome!

Make sure you aren’t storing that password plain-text. Built-in to Rails is the has_secure_password macro to help you with that.

Also if you are feeling a bit lost with regarding to authentication/sign-up there are several out-of-the-box addons for Rails that can remove that boilerplate. Unless you are doing something unique with your authentication/sign-up I might suggest consider it. See some options at: