基于 Devise 构建用户画像

Building User profile based on Devise

我正处于想要在我的网络应用程序中实现用户配置文件的阶段。我做了一些研究,并试图根据我读到的内容创造一些东西。我在使用 ProfilesControllers 时遇到了问题 - 我在任何已阅读的 post 中都没有遇到过这个问题。 所以我们开始吧:我关注了这些主题:Profile model for Devise users? and Creating Profile for Devise users and this Devise User Profile link_to.

我的错误是uninitialized constant ProfilesController

header.html.erb(布局)

  <header id="home">
    <div class="main-nav">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
            <%= link_to(image_tag('logo.png', :class => 'img-responsive'), root_path, :class => "navbar-brand") %> 
        </div>
        <div class="collapse navbar-collapse">
          <ul class="nav navbar-nav navbar-right">                 
            <li><%= link_to "Home", root_path, :class => "active" %></li>
            <li><%= link_to "View Profile", profile_path(current_user) %></li>
          </ul>
        </div>
      </div>
    </div><!--/#main-nav-->
  </header><!--/#home-->

routes.rb

Ims::Application.routes.draw do

  root 'pages#home'

  devise_for :users
  resources :articles



  get 'articles/index'
    authenticated :user do
    root :to => 'articles#index', :as => :authenticated_root
  end
  get '/:id', to: 'profiles#show', as: :profile
end

User.rb

  has_one :profile

  #Profile
  accepts_nested_attributes_for :profile 

Profile.rb

class Profile
  include Mongoid::Document

  belongs_to :user
    attr_accessible :uname
end

new.html.erb(设计注册)

<%= render 'layouts/header' %>

<div class="wrapper">
  <div class="signup">
  <p class="title">Register</p>

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
  <%= devise_error_messages! %>

    <i class="fa fa-user"></i>
      <div class="field">
        <%= f.label :username %><br />
        <%= f.text_field :username, autofocus: true %>
      </div>

      <div class="field">
        <%= f.label :email %><br />
        <%= f.email_field :email, autofocus: true %>
      </div>

      <div class="field">
        <%= f.label :password %>
        <% if @minimum_password_length %>
        <em>(<%= @minimum_password_length %> characters minimum)</em>
        <% end %><br />
        <%= f.password_field :password, autocomplete: "off" %>
      </div>

      <div class="field">
        <%= f.label :password_confirmation %><br />
        <%= f.password_field :password_confirmation, autocomplete: "off" %>
      </div>

      <div class="field">
        <%- if controller_name != 'sessions' %>
          Or <%= link_to "Log in", new_session_path(resource_name) %><br />
        <% end -%>
      </div>


<%= f.fields_for :profile do |profile_form| %>
  <h2><%= profile_form.label :uname %></h2>
  <p><%= profile_form.text_field :uname %></p>
<% end %>



      <div class="actions">
        <button>
          <%= f.submit "Register", :class => "login-button" %>
        </button>
      </div>

    <% end %>

  </div>
</div>

<%= render 'layouts/footer' %>

就是这样。如您所见,我没有 ProfilesController(我已经创建了它,但它是空的)。我真的不知道该怎么办。

顺便说一下,我正在使用 MongoID。 (如果重要的话)

最好的方法是在您的 User 模型上使用 before_create 构建 配置文件,然后您只需编辑它:

#app/models/user.rb
class User < ActiveRecord::Base
   has_one :profile
   before_create :build_profile
   accepts_nested_attributes_for :profile
end

要编辑它,请使用 singular resource:

#config/routes.rb
resource :profile, only: [:show, :update] #-> url.com/profile

#app/controllers/profiles_controller.rb
class ProfilesController < ApplicationController
   def show
   end

   def update
      current_user.update profile_params
   end

   private

   def profile_params
      params.require(:user).permit(profile_attributes: [])
   end
end

#app/views/profile/show.html.erb
<%= form_for current_user do |f| %>
   <%= f.fields_for :profile do |p| %>
      <%= p.text_field ... %>
   <% end %>
   <%= f.submit %>
<% end %>

就我个人而言,我会避免在 Devise 注册过程中添加任何额外内容;如果您希望他们编辑个人资料,请在成功后重定向用户 sign-up。