Skip to content

Commit d75a032

Browse files
committed
programs work.
1 parent c01fa31 commit d75a032

16 files changed

Lines changed: 484 additions & 3 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class ProgramsController < ApplicationController
2+
def index
3+
@programs = Program.all
4+
end
5+
6+
def show
7+
@program = Program.find(params[:id])
8+
end
9+
10+
def new
11+
@program = Program.new
12+
end
13+
14+
def edit
15+
@program = Program.find(params[:id])
16+
end
17+
18+
def create
19+
@program = Program.new(params[:program])
20+
@program.user = current_user
21+
22+
if @program.save
23+
flash[:notice] = 'Program was successfully created.'
24+
redirect_to(@program)
25+
else
26+
render :action => "new"
27+
end
28+
end
29+
30+
def update
31+
@program = Program.find(params[:id])
32+
33+
if @program.update_attributes(params[:program])
34+
flash[:notice] = 'Program was successfully updated.'
35+
redirect_to(@program)
36+
else
37+
render :action => "edit"
38+
end
39+
end
40+
41+
def destroy
42+
@program = Program.find(params[:id])
43+
@program.destroy
44+
end
45+
end

app/helpers/programs_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module ProgramsHelper
2+
end

app/models/program.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Program < ActiveRecord::Base
2+
belongs_to :user
3+
end

app/models/user.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ class User < ActiveRecord::Base
99

1010
has_many :inverse_messages, :class_name => "Message", :foreign_key => "recipient_id"
1111
has_many :inverse_recipients, :through => :inverse_messages, :source => :users
12+
13+
has_many :programs
1214
end

app/views/programs/edit.html.erb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<h1>Editing program</h1>
2+
3+
<% form_for(@program) do |f| %>
4+
<%= f.error_messages %>
5+
6+
<p>
7+
<%= f.label :name %><br />
8+
<%= f.text_field :name %>
9+
</p>
10+
<p>
11+
<%= f.label :description %><br />
12+
<%= f.text_field :description %>
13+
</p>
14+
<p>
15+
<%= f.label :text %><br />
16+
<%= f.text_area :text %>
17+
</p>
18+
<p>
19+
<%= f.label :user_id %><br />
20+
<%= f.text_field :user_id %>
21+
</p>
22+
<p>
23+
<%= f.submit 'Update' %>
24+
</p>
25+
<% end %>
26+
27+
<%= link_to 'Show', @program %> |
28+
<%= link_to 'Back', programs_path %>

app/views/programs/index.html.erb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<h1>Listing programs</h1>
2+
3+
<table>
4+
<tr>
5+
<th>Name</th>
6+
<th>Description</th>
7+
<th>Text</th>
8+
<th>User</th>
9+
</tr>
10+
11+
<% @programs.each do |program| %>
12+
<tr>
13+
<td><%=h program.name %></td>
14+
<td><%=h program.description %></td>
15+
<td><%=h program.text %></td>
16+
<td><%=h program.user_id %></td>
17+
<td><%= link_to 'Show', program %></td>
18+
<td><%= link_to 'Edit', edit_program_path(program) %></td>
19+
<td><%= link_to 'Destroy', program, :confirm => 'Are you sure?', :method => :delete %></td>
20+
</tr>
21+
<% end %>
22+
</table>
23+
24+
<br />
25+
26+
<%= link_to 'New program', new_program_path %>

app/views/programs/new.html.erb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<h1>New program</h1>
2+
3+
<% form_for(@program) do |f| %>
4+
<%= f.error_messages %>
5+
6+
<p>
7+
<%= f.label :name %><br />
8+
<%= f.text_field :name %>
9+
</p>
10+
<p>
11+
<%= f.label :description %><br />
12+
<%= f.text_field :description %>
13+
</p>
14+
<p>
15+
<%= f.label :text %><br />
16+
<%= f.text_area :text %>
17+
</p>
18+
<p>
19+
<%= f.submit 'Create' %>
20+
</p>
21+
<% end %>
22+
23+
<%= link_to 'Back', programs_path %>

app/views/programs/show.html.erb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<p>
2+
<b>Name:</b>
3+
<%=h @program.name %>
4+
</p>
5+
6+
<p>
7+
<b>Description:</b>
8+
<%=h @program.description %>
9+
</p>
10+
11+
<p>
12+
<b>Text:</b>
13+
<%=h @program.text %>
14+
</p>
15+
16+
<p>
17+
<b>User:</b>
18+
<%=h @program.user_id %>
19+
</p>
20+
21+
22+
<%= link_to 'Edit', edit_program_path(@program) %> |
23+
<%= link_to 'Back', programs_path %>

config/routes.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
ActionController::Routing::Routes.draw do |map|
2+
map.resources :programs
3+
24
map.resources :messages
35

46

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class CreatePrograms < ActiveRecord::Migration
2+
def self.up
3+
create_table :programs do |t|
4+
t.string :name
5+
t.string :description
6+
t.text :text
7+
t.integer :user_id
8+
9+
t.timestamps
10+
end
11+
end
12+
13+
def self.down
14+
drop_table :programs
15+
end
16+
end

0 commit comments

Comments
 (0)