Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails 4, Devise, and profile pages

I am new to coding, so this is probably a simple question.

I started using RoR about a month ago. Unfortunately, I've hit a bump and can't seem to get over it. I've tried looking at other SO questions for help, but I'm still a novice, so the coding suggestions still look a little foreign to me. I was hoping someone could put things into more novice-friendly terms.

What I want to do is have my website set up a profile for each user that signs up. This would be a private profile that only the user and admins would have access to. After the user signs up/logs in, I'd like for them to be redirected to their profile, where they can edit info like age and weight.

I've spent the last 3 days trying to figure out how to get a profile page created for each new user. I looked at the Devise github readme file, but I'm still stumped.

I've generated a users controller and users view, but I don't even know if I needed to do those steps since I have devise. Any help you guys could give me would be appreciated.

Here's a link to my github page - https://github.com/Thefoodie/PupPics

Thank you

like image 655
user3254679 Avatar asked Dec 01 '22 16:12

user3254679


1 Answers

Further to Kirti's answer, you'll need to actually have a profile to redirect to:

Models

#app/models/profile.rb
Class Profile < ActiveRecord::Base
    belongs_to :user
end

#app/models/user.rb
Class User < ActiveRecord::Base
    has_one :profile
    before_create :build_profile #creates profile at user registration
end

Schema

profiles
id | user_id | name | birthday | other | info | created_at | updated_at

Routes

#config/routes.rb
resources :profiles, only: [:edit]

Controller

#app/controllers/profiles_controller.rb
def edit
   @profile = Profile.find_by user_id: current_user.id
   @attributes = Profile.attribute_names - %w(id user_id created_at updated_at)
end

View

#app/views/profiles/edit.html.erb
<%= form_for @profile do |f| %>
    <% @attributes.each do |attr| %>
       <%= f.text_field attr.to_sym %>
    <% end %>
<% end %>

You'll then need to employ the after_sign_in_path stuff Kirti posted


Updated

Here is the migration you'd use:

# db/migrate/[[timestamp]]_create_profiles.rb
class CreateProfiles < ActiveRecord::Migration[5.0]
  def change
    create_table :profiles do |t|
      t.references :user
      # columns here
      t.timestamps
    end
  end
end
like image 175
Richard Peck Avatar answered Dec 26 '22 01:12

Richard Peck