Rails Auto Fi在注册和新配置文件中使用的类似字段



我的注册页面接受用户的输入first_namelast_nameemailaccount_type。当同一用户创建其个人资料页面时,我如何自动填写表单,以便:first_name等字段已经填写并节省时间?

Schema.rb

 create_table "profiles", force: :cascade do |t|
    t.string   "first_name"
    t.string   "last_name"
    t.string   "username"
    t.string   "gender"
    t.date     "birthday"
    t.string   "email"
    t.string   "account_type"
    t.datetime "created_at",        null: false
    t.datetime "updated_at",        null: false
    t.string   "handedness"
    t.string   "coach"
    t.date     "date_joined"
    t.integer  "year"
    t.string   "course"
    t.string   "main_weapon"
    t.string   "additional_weapon"
    t.integer  "cellphone_number"
    t.integer  "emergency_contact"
    t.string   "contact_name"
    t.string   "contact_rel"
    t.string   "player_status"
    t.integer  "user_id"
  end
create_table "users", force: :cascade do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    t.string   "first_name"
    t.string   "last_name"
    t.string   "username"
    t.string   "account_type"
    t.string   "security_key"
  end

_form视图:

<%= simple_form_for(@profile) do |f| %>
  <%= f.error_notification %>
  <div class="form-inputs">
    <%= f.input :first_name %>
    <%= f.input :last_name %>
    <%= f.input :username %>
    <%= f.input :gender, :collection => ['Male', 'Female'] %>
    <%= f.input :birthday %>
    <%= f.input :email %>
    <%= f.input :account_type %>
    <%= f.input :year %>
    <%= f.input :course %>
    <%= f.input :cellphone_number %>
    <%= f.input :handedness, :collection => ['Right','Left','Both']%>
    <%= f.input :coach %>
    <%= f.input :player_status, :collection => ['Active','Retired'] %>
    <%= f.input :main_weapon, :collection => ['Foil','Epee','Saber'] %>
    <%= f.input :additional_weapon, :collection => ['Foil','Epee','Saber'] %>
    <%= f.input :emergency_contact %>
    <%= f.input :contact_name %>
    <%= f.input :contact_rel %>


  </div>
  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>
你会

ProfilesController一个new的方法。只需在此时初始化字段,它们就会为您预填充表单字段。

class ProfilesController < ApplicationController
  def new
    @profile = Profile.new
    @profile.first_name = current_user.first_name
    @profile.last_name = current_user.last_name
    @profile.account_type = current_user.account_type
  end
  ...
end

最新更新