【问题标题】:how to output profile information from custom authentication如何从自定义身份验证中输出配置文件信息
【发布时间】:2013-03-19 16:54:49
【问题描述】:

我从头开始创建身份验证/授权。我需要帮助创建可以输出用户在注册时输入的信息以及他们的个人资料信息的页面。例如,当用户访问索引时,他们将看到注册信息(电子邮件、密码、用户、生日、邮政编码)。然后用户可以到 /users/ID-HERE 填写他们的个人资料信息(身高、孩子、职业、宗教、种族等),这是用户可以编辑个人资料信息的唯一页面。在这个页面 /users/ID-HERE/edit 用户可以修改他们的注册信息。

现在我没有一个输出用户输入信息的页面,它只显示编辑字段。所以我真正想要的是 /users/ID-HERE 显示用户输入的信息(不显示编辑字段)。然后 /users/ID-HERE/edit 将允许用户修改他们的个人资料信息。然后创建另一个 URL,/users/ID-HERE/account-edit 将允许用户修改他们的注册信息。并且在某个阶段,我会在构建时输出用户 CP 内的注册信息。

我不明白如何做到这一点。我只有页面 atm 允许您编辑帐户和个人资料信息,但没有单独的页面用于输出用户输入的纯文本信息。

routes.rb:

Dating::Application.routes.draw do
  get 'signup' => 'users#new'
  get 'login' => 'sessions#new'
  get 'logout' => 'sessions#destroy'
  get 'edit' => 'users#edit'
  get "/profile/:id" => "users#show"

  resources :users
  resources :sessions
  resources :password_resets
  resources :galleries
  resources :photos

show.html.erb(个人资料信息):

<h1><%= @user.username %></h1>

<h2>Basics</h2>

<%= form_for @user do |f| %>

    <div class="field">
        <%= f.label :height %><br/>
        <%= f.select :feet, [['Feet', nil], '4', '5', '6'] %>
        <%= f.select :inches, [['Inches', nil], '0', '1', '2', '3', '4',                    
                                '5', '6', '7', '8', '9', '10', '11'] %>
        </div>
    <div class="field">
        <%= f.label :children %><br/>
        <%= f.select :children, [['Do you have or want kids?', nil], 'Yes, they live with me', 'I want kids now', 'I want one someday', 'Not for me']%>
        </div>
    <div class="field">
        <%= f.label :religion %><br/>
        <%= f.select :religion, [['What is your faith?', nil], 'Agnostic', 'Atheist', 'Christian', 'Catholic', 'Buddhist', 'Hindu', 'Jewish', 'Muslim', 'Spiritual without affiliation', 'Other', 'None', 'Prefer not to say']%><br/>
        <%= f.select :religion, [['How important is this to you?', nil], 'Very Important', 'Somewhat Important', 'Not Important']%>
        </div>
    <div class="field">
        <%= f.label :career %><br/>
        <%= f.text_field :career %>
    </div>
    <div class="field">
        <%= f.label :education %><br/>
        <%= f.select :education, [['What is your education level?', nil], 'High school', 'Some college', 'Undergraduate', "Bachelor's", "Master's ", 'PhD', 'Business school', 'Law school', 'Medical school' ]%>
        </div>
    <div class="field">
        <%= f.label :ethnicity %><br/>
        <%= f.select :ethnicity, [['What is your ethnicity?', nil], 'Asian', 'Black', 'Biracial', 'Indian', 'Hispanic/Latin', 'Middle Eastern', 'Native American', 'Pacific Islander', 'White', 'Other' ]%>
        </div>
        <%= f.label :user_drink %><br/>
        <%= f.select :user_drink, [['How much do you drink?', nil], 'Often Drinks', 'Sometimes drinks', 'Never drinks', 'No comment' ]%>
        </div><br/>
        <%= f.label :user_smoke %><br/>
        <%= f.select :user_smoke, [['How often do you smoke?', nil], 'Often smokes', 'Sometimes smokes', 'Never smokes'] %>
        </div>
    <div class="actions"><%= f.submit %></div>

    <h3>About Me</h3>

    <%= form_for @user do |f| %>

    <div class="field">
        <%= f.label :about_me %><br/>
        <%= f.text_field :about_me %>
    <div class="actions"><%= f.submit %></div>

<% end %>
<% end %>

new.html.erb(注册表单):

注册

<%= form_for @user do |f| %>
  <% if @user.errors.any? %>
    <div class="error_messages">
      <h2>Form is invalid</h2>
      <ul>
        <% @user.errors.full_messages.each do |message| %>
          <li><%= message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

    <div class="field">
        <%= f.label :email %><br/>
        <%= f.text_field :email %>
    </div>
    <div class="field">
        <%= f.label :password %><br/>
        <%= f.password_field :password %>
    </div>
    <div class="field">
        <%= f.label :username %><br/>
        <%= f.text_field :username %>
    </div>
    <div class="field">
        <%= f.label :zip_code %><br/>
        <%= f.text_field :zip_code %>
    </div>
    <div class="field">
        <%= f.label :birthday %><br/>
        <%= f.text_field :birthday %>
    </div>
    <%= f.collection_select :role, User::ROLES, :to_s, :humanize %>
    <div class="actions"><%= f.submit %></div>
<% end %>

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    在你的users_controller:

    def show
      @user = User.find(params[:id])
    end
    

    现在您的@user 可以在show 视图中使用。因此,您不需要表单,只需:

    <h1><%= @user.username %></h1>
    
    <h2>Basics</h2>
      <p><%= @user.feet %></p>
      <p><%= @user.inches %></p>
      <p><%= @user.children %></p>
      <p><%= @user.religion %></p>
      ...   
    

    此外,资源将提供7 routes by default,因此您可以为其中的一些创建别名,而不是使用match:

    match '/signup', :to => 'users#new'
    match '/login',  :to => 'sessions#new'
    match '/logout', :to => 'sessions#destroy'
    
    resources :users
    resources :sessions
    resources :password_resets
    resources :galleries
    resources :photos
    

    【讨论】:

    • 感谢您对我的大力帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-01
    • 2022-12-10
    • 2014-12-27
    • 1970-01-01
    • 2020-12-20
    • 1970-01-01
    相关资源
    最近更新 更多