根据设计

时间:2016-01-26 13:30:26

标签: ruby-on-rails ruby devise mongoid user-profile

我想在我的网络应用程序中实现用户配置文件的阶段。我做了一些研究,并尝试根据我阅读的内容创建一些东西。我遇到了ProfilesControllers的问题 - 我没有在任何阅读过的帖子中遇到过这个问题。 我们开始:我遵循以下主题:Profile model for Devise users?Creating Profile for Devise users以及此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。 (如果重要的话)

1 个答案:

答案 0 :(得分:1)

最好的方法是使用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注册过程中添加任何额外内容;如果您希望他们编辑个人资料,只需在注册成功后重定向用户。