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 hit a blow and did not seem to handle it. I tried looking at other help questions, but I'm still a beginner, so the coding suggestions still seem a bit alien to me. I was hoping that someone could translate everything into more beginner-friendly conditions.

I want my site to set up a profile for each user who signs up. This will be a private profile that only users and administrators will have access to. After the user has registered / registered, I would like them to be redirected to their profile where they can edit information such as age and weight.

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

I created a user controller and users, but I don’t even know if I need to do these steps since I developed it. Any help you could give me would be appreciated.

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

thanks

+4
source share
2 answers

In addition to Kirti's answer, you will need to have profileto 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

Scheme

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 %>

after_sign_in_path , Kirti


:

# 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
+10

after_sign_in_path_for after_sign_up_path_for ApplicationController, profile. controller, profile.

: ( )

ApplicationController

  def after_sign_in_path_for(resource)
    profile_path(resource)
  end

  def after_sign_up_path_for(resource)
    profile_path(resource)
  end

ProfilesController

  ## You can skip this action if you are not performing any tasks but,
  ## It always good to include an action associated with a view.

  def profile

  end

, , view .

+8

All Articles