Domanda

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

È stato utile?

Soluzione

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

Altri suggerimenti

First you need to setup after_sign_in_path_for and after_sign_up_path_for for your resource in ApplicationController which will direct to the profile page. Then you need to create a controller which will render the profile page.

For Example: (Change it according to your requirement)

In ApplicationController define the paths

  def after_sign_in_path_for(resource)
    profile_path(resource)
  end

  def after_sign_up_path_for(resource)
    profile_path(resource)
  end

In ProfilesController

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

  def profile

  end

Also, make sure you create a view for user profile.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top