2017-05-15 1 views
0

私のサインアップページは、ユーザーからの入力としてfirst_name,last_nameemail、およびaccount_typeを入力します。同じユーザーが自分のプロフィールページを作成すると、:first_nameなどのフィールドがすでに埋められて時間が節約されるように、フォームを自動的に入力できますか?Rails自動サインアップと新しいプロフィールで使用される同様のフィールド

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 %> 
+0

モデルから引き出してフォームに挿入するコントローラ。 – Darkisa

+0

私の最初の質問は、なぜ列名を複製しているのでしょうか?ユーザーが複数のプロファイル(1対多の関係)を持つことは可能ですか? –

+0

いいえ私のユーザモデルに 'has_one:profile'があります – Kevin

答えて

2

あなたはProfilesControllernew方法を持っているでしょう。その時点でフィールドを初期化するだけで、フォームフィールドがあらかじめ入力されます。あなたは、フォームがロードにこれらの値を含める場合http://api.rubyonrails.org/v5.1.0/classes/ActionDispatch/Cookies.html、またはあなたが使用することができます。あなたはクッキーを使用する必要があります

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 
+0

何か奇妙なことが起こりました。ソリューションは機能しましたが、電子メールフィールドだけがあらかじめ入力されていました。残りは空白です – Kevin

+0

質問にプロフィールビューを表示できますか? – SteveTurczyn

+0

フォームを編集すると編集されます – Kevin

関連する問題