MacVim
最近は毎日Ruby on Rails Tutorialをやってます。教材として実に良い。
TDDメモ
migrationのtestする時に忘れない
$ rake db:migrate
$ rake db:test:prepare
参考
Active Record Nested Attributes
accepts_nested_attributes_forの簡単なまとめ
has_one関連でaccepts_nested_attributes_for / fields_forを使う
Railsのユーザ認証deviseを用いた簡単かつ突っ込んだ実装
Writing to a second model when using Devise
- stackoverflow
Profile model for Devise users? -これが一番わかり易かった。
Create a user profile upon registration add profile form fields to devise registrations#new form
How to add a UserProfile to a User when user signs up? (Devise, Rails 3)
No output through fields_for when using a one to one relationship
How to get devise to work with accepts_nested_attributes_for in a has one relationship?
How do I use nested attributes with the devise model
関連:
OmniAuth + Device で認証する①
OmniAuth + Device で認証する②
気になる
でもこの解決の仕方ってviewの中でモデル(コントローラ?)の仕事をしてるような気がしてちょっと気持ち悪い。幾つか他の方法もありそうなのだけど、そこまできにするのはやめた。
登録画面
ユーザが新規登録する際
# views/devise/registrations/new.html.erb<% resource.build_profile unless resource.profile %><%= f.fields_for :profile do |profile_form| %>
<%= profile_form.label :username %>
<%= profile_form.text_field :username %>
といったフォームを埋め込めば、profileがなければ新しく作った上で入力させることが出来ます。
後はユーザに紐付いたプロフィール表示とかその辺りを引き続きやっていきます。一旦メモ。
has_one, belongs_to, nested_attributes_for, profile_attributes
取り敢えず色々調べまくって至った結論。
# app/models/user.rb
has_one :profile
accepts_nested_attributes_for :profile
attr_accessible :... :profile_attributes
# app/models/profile.rb
belongs_to :user
一応これでユーザとプロフィールの関係性を明示して、accepts_nested...でネストしたprofileの変更をuserモデルが行えるようになりました。