Skip to content

Commit 07dd97b

Browse files
committed
Send messages to other users.
Users can send messages to other users.
1 parent 222c99d commit 07dd97b

6 files changed

Lines changed: 51 additions & 1 deletion

File tree

controllers/messages.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#people can get a form to send messages here
2+
get "/messages/new/to/:username" do
3+
#we've got to save the username to put it in the view
4+
@username = params[:username]
5+
6+
#render the template
7+
haml :"messages/new"
8+
end
9+
10+
#this is where the form POSTs to
11+
post "/messages" do
12+
#make a new message with our params
13+
message = Message.create(params[:message])
14+
15+
#set a friendly message
16+
flash[:notice] = "Message sent."
17+
18+
#render the page of the recipient
19+
redirect "/hackers/#{message.recipient}"
20+
end

features/messages.feature

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Feature: Messages
2+
Scenario: I can send a message
3+
Given I'm logged in
4+
And there's a hacker with the username "fela"
5+
And I go to the hacker page for "fela"
6+
When I follow "Send fela a message"
7+
And I fill in "Message text" with "Hello, fela!"
8+
And I press "Send message"
9+
Then I should be on the hacker page for "fela"
10+
And I should see "Message sent."
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-

models/message.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#this is the class for inter-site messages
2+
class Message
3+
#we want to include them in the database!
4+
include MongoMapper::Document
5+
6+
#this is the body text of the message
7+
key :body, String
8+
9+
#this is the username of the person who gets the message
10+
key :recipient, String
11+
12+
#this is the username of the person who sent the message
13+
key :sender, String
14+
end

views/hackers/show.haml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
%p= @hacker.username + "'s page"
2+
3+
%a{:href => "/messages/new/to/#{@hacker.username}" }= "Send #{@hacker.username} a message"

views/messages/new.haml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
%form{:action => "/messages", :method => "POST"}
2+
%input{:type => "hidden", :id => "message_recipient", :name => "message[recipient]", :value => @username}
3+
%label{:for => "message_text" } Message text:
4+
%textarea{:id => "message_text", :name => "message[text]"}
5+
%input{:type => "submit", :value => "Send message"}

0 commit comments

Comments
 (0)