Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions railway/app/assets/javascripts/tickets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
3 changes: 3 additions & 0 deletions railway/app/assets/stylesheets/tickets.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the Tickets controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
70 changes: 70 additions & 0 deletions railway/app/controllers/tickets_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
class TicketsController < ApplicationController
before_action :set_ticket, only: [:show, :edit, :update, :destroy]

# GET /tickets
# GET /tickets.json
def index
@tickets = Ticket.all
end

# GET /tickets/1
# GET /tickets/1.json
def show
end

# GET /tickets/new
def new
@ticket = Ticket.new
end

# GET /tickets/1/edit
def edit
end

# POST /tickets
# POST /tickets.json
def create
@ticket = Ticket.new(ticket_params)

respond_to do |format|
if @ticket.save
format.html { redirect_to @ticket, notice: 'Ticket was successfully created.' }
format.json { render :show, status: :created, location: @ticket }
else
format.html { render :new }
format.json { render json: @ticket.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /tickets/1
# PATCH/PUT /tickets/1.json
def update
respond_to do |format|
if @ticket.update(ticket_params)
format.html { redirect_to @ticket, notice: 'Ticket was successfully updated.' }
else
format.html { render :edit }
end
end
end

# DELETE /tickets/1
# DELETE /tickets/1.json

def destroy
@ticket.destroy
redirect_to tickets_path
end

private
# Use callbacks to share common setup or consticketts between actions.
def set_ticket
@ticket = Ticket.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def ticket_params
params.require(:ticket).permit(:f_name, :l_name, :route_id, :f_station_id, :l_station_id)
end
end
2 changes: 2 additions & 0 deletions railway/app/helpers/tickets_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module TicketsHelper
end
1 change: 1 addition & 0 deletions railway/app/models/railway_station.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ class RailwayStation < ActiveRecord::Base
has_many :trains
has_many :railway_stations_routes
has_many :routes, through: :railway_stations_routes
has_many :tickets

end
6 changes: 4 additions & 2 deletions railway/app/models/ticket.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Ticket < ActiveRecord::Base
belongs_to :train
belongs_to :user
belongs_to :route
belongs_to :f_station, class_name: 'RailwayStation', foreign_key: :f_station_id
belongs_to :l_station, class_name: 'RailwayStation', foreign_key: :l_station_id
#belongs_to :user
end
2 changes: 1 addition & 1 deletion railway/app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class User < ActiveRecord::Base
has_many :tickets
#has_many :tickets
end
22 changes: 22 additions & 0 deletions railway/app/views/tickets/_form.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
= form_for(@ticket) do |f|
= render 'common/errors', resource: @ticket

.field
= f.label :f_name
= f.text_field :f_name
.field
= f.label :l_name
= f.text_field :l_name
.field
= f.label :route, "Route"
= f.collection_select :route_id, Route.all, :id, :title

.field
= f.label :f_station, "first station"
= f.collection_select :f_station_id, RailwayStation.all, :id, :title
.field
= f.label :l_station, "last station"
= f.collection_select :l_station_id, RailwayStation.all, :id, :title

.actions
= f.submit "#{@ticket.new_record? ? 'Create' : 'Change'}"
5 changes: 5 additions & 0 deletions railway/app/views/tickets/_ticket.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
tr
td = "#{ticket.f_name} #{ticket.l_name} "
td = link_to 'Show', ticket
td = link_to 'Edit', edit_ticket_path(ticket)
td = link_to 'Destroy', ticket, data: { :confirm => 'Are you sure?' }, :method => :delete
7 changes: 7 additions & 0 deletions railway/app/views/tickets/edit.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
h1 Editing Train

== render 'form'

= link_to 'Show', @ticket
'|
= link_to 'Back', tickets_path
15 changes: 15 additions & 0 deletions railway/app/views/tickets/index.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
p#notice = notice

h1 Listing Tickets

table
thead
tr
th Title
th colspan="3"

tbody
= render @tickets
br

= link_to 'New Ticket', new_ticket_path
5 changes: 5 additions & 0 deletions railway/app/views/tickets/new.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
h1 New Train

== render 'form'

= link_to 'Back', tickets_path
27 changes: 27 additions & 0 deletions railway/app/views/tickets/show.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
p#notice = notice

p
strong Name:
= @ticket.f_name
= @ticket.l_name

p
strong Route:
- if @ticket.route.present?
= @ticket.route.title
- else
.not Not point route

p
strong Stations:
- if @ticket.f_station.present? && @ticket.l_station.present?
= @ticket.f_station.title
'|
= @ticket.l_station.title
- else
.not Not point stations


= link_to 'Edit', edit_ticket_path(@ticket)
'|
= link_to 'Back', tickets_path
2 changes: 2 additions & 0 deletions railway/app/views/welcome/index.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ h1 Welcome Rails!
= link_to 'Route', routes_path
'|
= link_to 'Wagon', wagons_path
'|
= link_to 'Ticket', tickets_path
1 change: 1 addition & 0 deletions railway/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
resources :routes
resources :wagons
resources :utypes
resources :tickets

get 'welcome/index'

Expand Down
8 changes: 8 additions & 0 deletions railway/db/migrate/20160313164948_add_tikets_field.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class AddTiketsField < ActiveRecord::Migration
def change
change_table :tickets do |t|
t.string :f_name
t.string :l_name
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RemoveUserIdToTickets < ActiveRecord::Migration
def change
remove_belongs_to :tickets, :user
end
end
7 changes: 7 additions & 0 deletions railway/db/migrate/20160313170114_add_route_id_to_tickets.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class AddRouteIdToTickets < ActiveRecord::Migration
def change
add_belongs_to :tickets, :route
add_belongs_to :tickets, :f_station
add_belongs_to :tickets, :l_station
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RemoveTrainIdToTickets < ActiveRecord::Migration
def change
remove_belongs_to :tickets, :train
end
end
13 changes: 8 additions & 5 deletions railway/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160311152424) do
ActiveRecord::Schema.define(version: 20160313170445) do

create_table "railway_stations", force: :cascade do |t|
t.string "title"
Expand All @@ -31,10 +31,13 @@
end

create_table "tickets", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.integer "train_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "f_name"
t.string "l_name"
t.integer "route_id"
t.integer "f_station_id"
t.integer "l_station_id"
end

create_table "trains", force: :cascade do |t|
Expand Down
7 changes: 7 additions & 0 deletions railway/test/controllers/tickets_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class TicketsControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
end