Skip to content

Instantly share code, notes, and snippets.

@mpattee
Created February 12, 2012 18:31
Show Gist options
  • Save mpattee/1810059 to your computer and use it in GitHub Desktop.
Save mpattee/1810059 to your computer and use it in GitHub Desktop.
My attempt at 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 :quantity %>: <%= f.text_field :quantity %></li>
<li><%= f.label :price %>: <%= f.text_field :price %></li>
<li><%= f.label :weight %>: <%= f.text_field :weight %></li>
<li><%= f.label :description %>: <%= f.text_area :itemDescription %></li>
</ul>
<% form_for @item, { :url => user_character_item_url(@user, @character, @item.id) } do |f| %>
<%= render :partial => 'form', :locals => { :f => f } %>
<%= "<iframe src=\"#{@item.url}\"></iframe>" if not @item.url.empty? %>
<div id="navigation">
<%= link_to image_tag('back.png'), user_character_url(@user, @character, { :section => 'items' }) %>
<%= image_submit_tag 'update.png', :class => 'submit' %>
</div>
<% end %>
class Item < ActiveRecord::Base
belongs_to :character
has_friendly_id :name, :use_slug => true, :scope => :character
validates_presence_of :name, :quantity
end
class ItemsController < ApplicationController
def new
@user = User.find(params[:user_id])
@character = @user.characters.find(params[:character_id])
@item = @character.items.new
end
def create
@user = User.find(params[:user_id])
@character = @user.characters.find(params[:character_id])
@item = @character.items.new(params[:item])
if @item.save
flash[:pass] = "Item \"#{@item.name}\" was successfully created."
redirect_to user_character_url(@user, @character, { :section => 'items' })
else
flash[:fail] = 'Item creation failed.'
render :action => "new"
end
end
def edit
@user = User.find(params[:user_id])
@character = @user.characters.find(params[:character_id])
@item = @character.items.find(params[:id])
end
def update
@user = User.find(params[:user_id])
@character = @user.characters.find(params[:character_id])
@item = @character.items.find(params[:id])
RAILS_DEFAULT_LOGGER.info("PARAMS: #{params[:item]}")
if @item.update_attributes(params[:item])
flash[:pass] = "Item \"#{@item.name}\" successfully updated."
redirect_to user_character_url(@user, @character, { :section => 'items' })
else
flash[:fail] = "Item update failed.#{@item.errors} test"
render :action => 'edit'
end
end
def destroy
@user = User.find(params[:user_id])
@character = @user.characters.find(params[:character_id])
@item = @character.items.find(params[:id])
if @item.destroy
flash[:pass] = "Item \"#{@item.name}\" successfully deleted."
else
flash[:fail] = 'Item deletion failed.'
end
redirect_to user_character_url(@user, @character, { :section => 'items' })
end
end
SQL (0.3ms) SET NAMES 'utf8'
SQL (0.2ms) SET SQL_AUTO_IS_NULL=0
Item Columns (8.7ms) SHOW FIELDS FROM `items`
Item Load (0.3ms) SELECT * FROM `items` WHERE (`items`.`id` = 50568) LIMIT 1
SQL (0.1ms) BEGIN
SQL (0.1ms) ROLLBACK
DEPRECATION WARNING: Giving :session_key to SessionStore is deprecated, please use :key instead. (called from new at /opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.11/lib/action_controller/middleware_stack.rb:72)
SQL (0.1ms) SET NAMES 'utf8'
SQL (0.1ms) SET SQL_AUTO_IS_NULL=0
Processing ItemsController#update (for 127.0.0.1 at 2012-02-12 10:46:26) [PUT]
Parameters: {"x"=>"82", "y"=>"18", "authenticity_token"=>"Q9BAA8LaWOnSCy5WIW2tDiQKvKJzMbcSYv0tvO/ZdY0=", "id"=>"50568", "user_id"=>"mpatteegmailcom", "character_id"=>"20370", "item"=>{"price"=>"", "name"=>"New Item", "itemDescription"=>"", "quantity"=>"", "weight"=>"", "url"=>""}}
User Columns (4.2ms) SHOW FIELDS FROM `users`
User Load (0.5ms) SELECT `users`.* FROM `users` INNER JOIN `slugs` ON `slugs`.sluggable_id = `users`.id AND `slugs`.sluggable_type = 'User' WHERE (`slugs`.`sequence` = 1 AND `slugs`.`name` = 'mpatteegmailcom') LIMIT 1
Character Columns (2.1ms) SHOW FIELDS FROM `characters`
Character Load (0.3ms) SELECT * FROM `characters` WHERE (`characters`.`id` = 20370 AND (`characters`.user_id = 1))
Item Load (14.8ms) SELECT `items`.* FROM `items` INNER JOIN `slugs` ON `slugs`.sluggable_id = `items`.id AND `slugs`.sluggable_type = 'Item' WHERE (`items`.character_id = 20370) AND ((`slugs`.`scope` IS NULL AND `slugs`.`sequence` = 1 AND `slugs`.`name` = '50568')) ORDER BY name LIMIT 1
Item Columns (0.9ms) SHOW FIELDS FROM `items`
Item Load (0.2ms) SELECT * FROM `items` WHERE (`items`.`id` = 50568 AND (`items`.character_id = 20370)) ORDER BY name LIMIT 1
PARAMS: nameNew ItempricequantityitemDescriptionurlweight
SQL (0.1ms) BEGIN
SQL (0.1ms) ROLLBACK
Rendering template within layouts/application
Rendering items/edit
Slug Load (0.3ms) SELECT * FROM `slugs` WHERE (`slugs`.sluggable_id = 1 AND `slugs`.sluggable_type = 'User') ORDER BY id DESC LIMIT 1
Rendered items/_form (102.5ms)
Completed in 224ms (View: 122, DB: 24) | 200 OK [http://localhost/users/mpatteegmailcom/characters/20370/items/50568]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment