Ruby on Rails 4, Devise, and profile pages

别等时光非礼了梦想. 提交于 2019-11-30 10:15:37

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!