我有兩個型號更新HAS_ONE關係模型
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
下面是編輯和
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
我想我只需要找到記錄和編輯,但沒有我的user_information控制器的更新操作。任何人都可以幫助我嗎?
拼寫User_informations#edit'是讓我信服。你能顯示控制器類名和文件名嗎? – ck3g
確定控制器名稱是UserInformationsController,文件名是user_informations_controller和編輯記錄的路徑是http://本地主機:3000 /用戶/ 1/user_informations/1 /編輯 – Jean
所以,你的鏈接幫助應該是'users_user_information(USER_ID, user_info_id)'。助手的名字可以不同,但要注意兩個參數。 – ck3g