Skip to content

Instantly share code, notes, and snippets.

@jdhuntington
Forked from mpattee/_form.rb
Created February 24, 2012 16:48
Show Gist options
  • Save jdhuntington/1901969 to your computer and use it in GitHub Desktop.
Save jdhuntington/1901969 to your computer and use it in GitHub Desktop.
Unexpected lack of validation
<ul>
<li><%= f.label :name %>: <%= f.text_field :name %></li>
<li><%= f.label :url %>: <%= f.text_field :url, :size => 100 %></li>
<li><%= f.label :slot %>: <%= f.select :equipmentSlot, @slots %></li>
<li><%= f.label :weight %>: <%= f.text_field :weight %></li>
<li><%= f.label :price %>: <%= f.text_field :price %></li>
<li><%= f.label :rarity %>: <%= f.text_field :rarity %></li>
<li><%= f.label :speed %>: <%= f.text_field :speed %></li>
<li><%= f.label :armor_type %>: <%= f.text_field :armorType %></li>
<li><%= f.label :armor_check %>: <%= f.text_field :armorCheck %></li>
<li><%= f.label :enhancement_bonus %>: <%= f.text_field :enhancementBonus %></li>
<li><%= f.label :armor_bonus %>: <%= f.text_field :armorBonus %></li>
<li><%= f.label :is_light_armor %>: <%= f.check_box :isLightArmor %></li>
<li><%= f.label :is_equipped %>: <%= f.check_box :isEquipped %></li>
<li><%= f.label :property %>: <%= f.text_area :property %></li>
</ul>
class Equipment < ActiveRecord::Base
belongs_to :character
has_friendly_id :name, :use_slug => true, :scope => :character
validates_presence_of :name, :speed# , :armorCheck, :enhancementBonus, :armorBonus, :level
validates_numericality_of :speed# , :armorCheck, :enhancementBonus, :armorBonus
class Slots
UNKNOWN = 0
ARMOR = 1
ARMS = 2
FEET = 3
HANDS = 4
HEAD = 5
NECK = 6
RINGS = 7
TATTOO = 8
WAIST = 9
WONDROUS = 10
end
def after_save
character.defenses.each do |defense|
defense.calculate_total
end
end
end
class EquipmentsController < ApplicationController
def new
@user = User.find(params[:user_id])
@character = @user.characters.find(params[:character_id])
@equipment = @character.equipments.new
initialize_slots
end
def create
@user = User.find(params[:user_id])
@character = @user.characters.find(params[:character_id])
@equipment = @character.equipments.new(params[:equipment])
initialize_slots
if @equipment.save
flash[:pass] = "Equipment \"#{@equipment.name}\" was successfully created."
redirect_to user_character_url(@user, @character, { :section => 'items' })
else
flash[:fail] = 'Equipment creation failed.'
render :action => "new"
end
end
def edit
@user = User.find(params[:user_id])
@character = @user.characters.find(params[:character_id])
@equipment = @character.equipments.find(params[:id])
initialize_slots
logger.info("EQUIPMENT URL: #{@equipment.url}")
end
def update
@user = User.find(params[:user_id])
@character = @user.characters.find(params[:character_id])
@equipment = @character.equipments.find(params[:id])
initialize_slots
if @equipment.update_attributes(params[:equipment])
flash[:pass] = "Equipment \"#{@equipment.name}\" successfully updated."
redirect_to user_character_url(@user, @character, { :section => 'items' })
else
flash[:fail] = 'Equipment update failed. #{@equipment.errors.full_messages.join(', ')}'
render :action => 'edit'
end
end
def destroy
@user = User.find(params[:user_id])
@character = @user.characters.find(params[:character_id])
@equipment = @character.equipments.find(params[:id])
initialize_slots
if @equipment.destroy
flash[:pass] = "Equipment \"#{@equipment.name}\" successfully deleted."
else
flash[:fail] = 'Equipment deletion failed.'
end
redirect_to user_character_url(@user, @character, { :section => 'items' })
end
private
def initialize_slots
@slots = '<option '+(@equipment.equipmentSlot == 0 ? 'SELECTED' : '')+' value="0">Unknown</option>'
@slots += '<option '+(@equipment.equipmentSlot == 1 ? 'SELECTED' : '')+' value="1">Armor</option>'
@slots += '<option '+(@equipment.equipmentSlot == 2 ? 'SELECTED' : '')+' value="2">Arms</option>'
@slots += '<option '+(@equipment.equipmentSlot == 3 ? 'SELECTED' : '')+' value="3">Feet</option>'
@slots += '<option '+(@equipment.equipmentSlot == 4 ? 'SELECTED' : '')+' value="4">Hands</option>'
@slots += '<option '+(@equipment.equipmentSlot == 5 ? 'SELECTED' : '')+' value="5">Head</option>'
@slots += '<option '+(@equipment.equipmentSlot == 6 ? 'SELECTED' : '')+' value="6">Neck</option>'
@slots += '<option '+(@equipment.equipmentSlot == 7 ? 'SELECTED' : '')+' value="7">Rings</option>'
@slots += '<option '+(@equipment.equipmentSlot == 8 ? 'SELECTED' : '')+' value="8">Tattoo</option>'
@slots += '<option '+(@equipment.equipmentSlot == 9 ? 'SELECTED' : '')+' value="9">Waist</option>'
@slots += '<option '+(@equipment.equipmentSlot == 10 ? 'SELECTED' : '')+' value="10">Wondrous</option>'
end
end
NoMethodError in Equipments#update
Showing app/views/equipments/_form.erb where line #4 raised:
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.inject
Extracted source (around line #4):
1: <ul>
2: <li><%= f.label :name %>: <%= f.text_field :name %></li>
3: <li><%= f.label :url %>: <%= f.text_field :url, :size => 100 %></li>
4: <li><%= f.label :slot %>: <%= f.select :equipmentSlot, @slots %></li>
5: <li><%= f.label :weight %>: <%= f.text_field :weight %></li>
6: <li><%= f.label :price %>: <%= f.text_field :price %></li>
7: <li><%= f.label :rarity %>: <%= f.text_field :rarity %></li>
Trace of template inclusion: app/views/equipments/edit.html.erb
RAILS_ROOT: /Users/mpattee/Documents/source/i4e_import
Application Trace | Framework Trace | Full Trace
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.11/lib/action_view/helpers/form_options_helper.rb:292:in `options_for_select'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.11/lib/action_view/helpers/form_options_helper.rb:533:in `to_select_tag'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.11/lib/action_view/helpers/form_options_helper.rb:128:in `select'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.11/lib/action_view/helpers/form_options_helper.rb:583:in `select'
/Users/mpattee/Documents/source/i4e_import/app/views/equipments/_form.erb:4:in `_run_erb_app47views47equipments47_form46erb_locals_f_form_object'
/Users/mpattee/Documents/source/i4e_import/app/views/equipments/edit.html.erb:2:in `_run_erb_app47views47equipments47edit46html46erb'
/Users/mpattee/Documents/source/i4e_import/app/views/equipments/edit.html.erb:1:in `_run_erb_app47views47equipments47edit46html46erb'
/Users/mpattee/Documents/source/i4e_import/app/controllers/equipments_controller.rb:64:in `update'
Request
Parameters:
{"x"=>"44",
"y"=>"17",
"_method"=>"put",
"authenticity_token"=>"FzsxHw8/3SlumMleg2Q/hAdUURgn3DLkshW/YpCZiJA=",
"id"=>"62577",
"user_id"=>"mpatteegmailcom",
"equipment"=>{"name"=>"Delver's Leather Armor +2",
"price"=>"",
"isEquipped"=>"1",
"property"=>"",
"rarity"=>"Common",
"armorBonus"=>"2",
"armorType"=>"Leather",
"url"=>"http://www.wizards.com/dndinsider/compendium/item.aspx?fid=558&ftype=1",
"weight"=>"15",
"equipmentSlot"=>"1",
"speed"=>"",
"enhancementBonus"=>"2",
"armorCheck"=>"0",
"isLightArmor"=>"1"},
"character_id"=>"20385"}
Show session dump
Response
Headers:
{"Content-Type"=>"text/html",
"Cache-Control"=>"no-cache"}
@mpattee
Copy link

mpattee commented Feb 24, 2012

NoMethodError in EquipmentsController#update

undefined method `equipmentSlot' for nil:NilClass
RAILS_ROOT: /Users/mpattee/Documents/source/i4e_import

Application Trace | Framework Trace | Full Trace
/opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.11/lib/active_support/whiny_nil.rb:52:in method_missing' /Users/mpattee/Documents/source/i4e_import/app/controllers/equipments_controller.rb:61:ininitialize_slots'
Request

Parameters:

{"x"=>"44",
"y"=>"16",
"_method"=>"put",
"authenticity_token"=>"FzsxHw8/3SlumMleg2Q/hAdUURgn3DLkshW/YpCZiJA=",
"id"=>"62577",
"user_id"=>"mpatteegmailcom",
"equipment"=>{"name"=>"Delver's Leather Armor +2",
"price"=>"",
"isEquipped"=>"1",
"property"=>"",
"rarity"=>"Common",
"armorBonus"=>"2",
"armorType"=>"Leather",
"url"=>"http://www.wizards.com/dndinsider/compendium/item.aspx?fid=558&ftype=1",
"weight"=>"15",
"equipmentSlot"=>"1",
"speed"=>"",
"enhancementBonus"=>"2",
"armorCheck"=>"0",
"isLightArmor"=>"1"},
"character_id"=>"20385"}
Show session dump

Response

Headers:

{"Content-Type"=>"",
"Cache-Control"=>"no-cache"}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment