使用 Rails 3.1.0
def create
@practice = Practice.new(params[:practice])
respond_to do |format|
if (current_user.practices << @practice rescue false)
pmf = current_user.practices_users.inspect # if this line is removed the following update_attributes method breaks!
current_user.practices_users.last.update_attributes(:admin_flg => true, :first_name => params[:first_name], :last_name => params[:last_name])
format.html { redirect_to home_dashboard_path, notice: 'Practice was successfully created.' }
format.json { render json: @practice, status: :created, location: @practice }
else
format.html { render action: "new" }
format.json { render json: @practice.errors, status: :unprocessable_entity }
end
end
end
当'pmf = ...'行不存在时,我得到这条线
NoMethodError:
undefined method `update_attributes' for nil:NilClass
当存在“pmf = ...”行时,创建操作正常工作。到底是怎么回事?