典型的な User モデルと Profile モデルを持つシンプルなアプリを構築しています。ユーザーにはプロファイルが 1 つあり、プロファイルはユーザーに属します。私は基本的にMichael Hartlのチュートリアルに従っているので、すべてうまくいっているようです。ただし、プロファイル テーブル (アクションを表示) から何かのビューをレンダリングしようとすると、エラー (ID なし) が発生し、作成したプロファイル レコードが削除されます!
質問:
ProfilesController で、レンダリングしようとしている単純なビューに対して show アクションを適切に定義していますか?
URL localhost/3000/profiles/1 にアクセスするだけでプロファイル レコードが削除されるのはなぜですか? 私はそれが依存破壊と関係があると思います(この動作を停止するb / c削除)が、依存破壊を維持したいと思いますよね?
ルート
resources :users
resources :profiles
モデル
Class User < ActiveRecord::Base
has_one :profile, dependent: :destroy
Class Profile < ActiveRecord::Base
belongs_to :user
プロフィールコントローラー
def new
@profile = current_user.build_profile
end
def create
@profile = current_user.build_profile(params[:profile])
if @profile.save
flash[:success] = "Profile created dude!"
redirect_to root_path
else
render 'new'
end
end
def show
@profile = Profile.find(params[:user_id])
end
見る (プロフィール/show.html.erb)
<p>Display Name: <%= @profile.display_name %></p>