Created
January 28, 2013 12:29
-
-
Save fabianoalmeida/4655153 to your computer and use it in GitHub Desktop.
Structure based on friendly_id
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>Places</h1> | |
<ul> | |
<% @places.each do |place| %> | |
<li><%= link_to place.name, [:new, place, :ticket] %></li> | |
<% end %> | |
</ul> | |
=========================================================================== | |
<h1><%= "New Ticket for %s" % @place.name %></h1> | |
<%= form_for [@place, @ticket] do |f| %> | |
<p> | |
<%= f.label :ticket_type_id %> | |
<%= f.collection_select :ticket_type_id, TicketType.all, :id, :name %> | |
</p> | |
<p><%= f.submit %></p> | |
<% end %> | |
=========================================================================== | |
<h1><%= 'Ticket #%d' % @ticket.id %></h1> | |
<p>Place: <b><%= @ticket.place_name %></b></p> | |
<p>Type: <b><%= @ticket.ticket_type_name %></b></p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Place < ActiveRecord::Base | |
has_many :tickets | |
attr_accessible :name | |
extend FriendlyId | |
friendly_id :name, use: :slugged | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PlacesController < ApplicationController | |
def index | |
@places = Place.all | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
place_tickets POST /places/:place_id/tickets(.:format) tickets#create | |
new_place_ticket GET /places/:place_id/tickets/new(.:format) tickets#new | |
places GET /places(.:format) places#index | |
ticket GET /tickets/:id(.:format) tickets#show | |
root / places#index |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Fabianoalmeida::Application.routes.draw do | |
resources :places, only: [:index] do | |
resources :tickets, only: [:new, :create] | |
end | |
resources :tickets, only: [:show] | |
root :to => 'places#index' | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ActiveRecord::Schema.define(:version => 20121206233226) do | |
create_table "places", :force => true do |t| | |
t.string "name" | |
t.string "slug" | |
t.datetime "created_at", :null => false | |
t.datetime "updated_at", :null => false | |
end | |
add_index "places", ["slug"], :name => "index_places_on_slug" | |
create_table "ticket_types", :force => true do |t| | |
t.string "name" | |
t.string "slug" | |
t.datetime "created_at", :null => false | |
t.datetime "updated_at", :null => false | |
end | |
add_index "ticket_types", ["slug"], :name => "index_ticket_types_on_slug" | |
create_table "tickets", :force => true do |t| | |
t.integer "place_id" | |
t.integer "ticket_type_id" | |
t.datetime "created_at", :null => false | |
t.datetime "updated_at", :null => false | |
end | |
add_index "tickets", ["place_id"], :name => "index_tickets_on_place_id" | |
add_index "tickets", ["ticket_type_id"], :name => "index_tickets_on_ticket_type_id" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'test_helper' | |
class PlacesControllerTest < ActionController::TestCase | |
test "should get index" do | |
get :index | |
assert_response :success | |
assert_not_nil assigns(:places) | |
end | |
end | |
class TicketsControllerTest < ActionController::TestCase | |
test "should get new with place" do | |
get :new, place_id: places(:one) | |
assert_response :success | |
assert_not_nil assigns(:ticket) | |
assert_equal places(:one), assigns(:ticket).place | |
end | |
test "should create with place" do | |
post :create, place_id: places(:one), ticket: { ticket_type_id: ticket_types(:one) } | |
assert_redirected_to assigns(:ticket) | |
assert_equal places(:one), assigns(:ticket).place | |
end | |
test "should get show" do | |
get :show, id: tickets(:one) | |
assert_response :success | |
assert_equal tickets(:one), assigns(:ticket) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Ticket < ActiveRecord::Base | |
belongs_to :place | |
belongs_to :ticket_type | |
attr_accessible :ticket_type_id | |
delegate :name, to: :place, prefix: true | |
delegate :name, to: :ticket_type, prefix: true | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TicketType < ActiveRecord::Base | |
has_many :tickets | |
attr_accessible :name | |
extend FriendlyId | |
friendly_id :name, use: :slugged | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TicketsController < ApplicationController | |
before_filter :find_place, only: [:new, :create] | |
def new | |
@ticket = @place.tickets.build | |
end | |
def create | |
@ticket = @place.tickets.build(params[:ticket]) | |
if @ticket.save | |
redirect_to @ticket, notice: "Successfully created ticket." | |
else | |
render action: "new" | |
end | |
end | |
def show | |
@ticket = Ticket.find(params[:id]) | |
end | |
private | |
def find_place | |
@place = Place.find(params[:place_id]) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment