私は2つのモデルを持っています
class User < ActiveRecord::Base
has_one :user_information, :dependent => :destroy
attr_accessible :email, :name
end
と
class UserInformation < ActiveRecord::Base
belongs_to :user
attr_accessible :address, :business, :phone, :user_id
end
ユーザーを作成した後、コントローラーの新規アクションと作成アクションを使用してユーザー情報を作成しました。
def new
@user = User.find(params[:id])
@user_information = @user.build_user_information
respond_to do |format|
format.html # new.html.erb
format.json { render json: @user_information }
end
end
def create
@user_information = UserInformation.new(params[:user_information])
respond_to do |format|
if @user_information.save
format.html { redirect_to @user_information, notice: 'User information was successfully created.' }
format.json { render json: @user_information, status: :created, location: @user_information }
else
format.html { render action: "new" }
format.json { render json: @user_information.errors, status: :unprocessable_entity }
end
end
end
すべて正常に動作しますが、レコードを更新しようとすると、次のエラーが発生します。
RuntimeError in User_informations#edit
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
これが私のuser_informationコントローラーの編集と更新アクションです
def edit
@user_information = UserInformation.find(params[:id])
end
def update
@user_information = UserInformation.find(params[:id])
respond_to do |format|
if @user_information.update_attributes(params[:user_information])
format.html { redirect_to @user_information, notice: 'User information was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @user_information.errors, status: :unprocessable_entity }
end
end
end
レコードを見つけて編集するだけでいいと思いましたが、違います。誰か助けてくれませんか?