Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

now users can send messages

  • Loading branch information...
commit ce0b3659628a57b6dce779b21782df3d844d959b 1 parent 8bd10e6
strix3000 strix3000 authored
3  app/assets/javascripts/message.js.coffee
View
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
3  app/assets/stylesheets/message.css.scss
View
@@ -0,0 +1,3 @@
+// Place all the styles related to the message controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
37 app/controllers/messages_controller.rb
View
@@ -0,0 +1,37 @@
+class MessagesController < ApplicationController
+
+def create
+ @recipient = User.find(params[:id])
+ @message = Message.new()
+ @message.content = params[:content]
+ @message.user = current_user
+ @message.save
+ mes_recipient = MessageRecipient.new()
+ mes_recipient.message_id = @message.id
+ mes_recipient.user_id = 1
+ mes_recipient.save
+end
+
+def show
+ @message = Message.find(params[:id])
+end
+
+def index
+ @messages = current_user.messages + current_user.recieved_messages
+end
+
+def update
+ @message = Message.find(params[:id])
+
+ respond_to do |format|
+ if @message.update_attributes(params[:message])
+ format.html { redirect_to @message, notice: 'message was successfully updated.' }
+ format.json { head :ok }
+ else
+ format.html { render action: "edit" }
+ format.json { render json: @message.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+end
2  app/controllers/uploads_controller.rb
View
@@ -39,7 +39,7 @@ def edit
# POST /files.json
def create
@file = Upload.new(params[:upload])
- @file.user = current_user
+ @file.user = current_user
respond_to do |format|
if @file.save
format.html { redirect_to(:uploads, :notice => 'file was successfully created.') }
2  app/helpers/message_helper.rb
View
@@ -0,0 +1,2 @@
+module MessageHelper
+end
9 app/models/message.rb
View
@@ -0,0 +1,9 @@
+class Message < ActiveRecord::Base
+#Relations
+ belongs_to :user
+ has_many :message_recipients
+ has_many :recipients, :through => :message_recipients, :source => :user
+#Validations
+ validate :user_id, :presence => true
+
+end
10 app/models/message_recipient.rb
View
@@ -0,0 +1,10 @@
+class MessageRecipient < ActiveRecord::Base
+#Relations
+ belongs_to :message
+ belongs_to :user
+
+#Validations
+ validate :user_id, :presence => true
+ validate :message_id , :presence => true
+
+end
8 app/models/post.rb.save
View
@@ -1,8 +0,0 @@
-lass Post < ActiveRecord::Base
- attr_accesible :content
- belongs_to :user
- validates :content, :presence => true, :length => { :maximum => 2000 }
- validates :user_id, :presence => true
- default_scope :order => 'posts.created_at DESC'
-
-end
9 app/models/user.rb
View
@@ -4,12 +4,13 @@ class User < ActiveRecord::Base
#Relations
has_many :uploads
-
has_many :users_upload
has_many :saved_uploads, :through => :users_upload, :source => :upload
-has_many :posts, :dependent => :destroy
-# has_many :uploads, :through => :users_uploads
-
+ has_many :posts, :dependent => :destroy
+
+ has_many :messages
+ has_many :message_recipient
+ has_many :recieved_messages, :through => :message_recipient, :source => :message
15 app/views/layouts/_header.html.erb
View
@@ -1,16 +1,19 @@
- <header>
- <%= image_tag("logo.png", :alt => "Ruby School", :class => "round") %>
+<header>
+ <%= image_tag("logo.png", :alt => "Ruby School", :class => "round") %>
<nav class="round">
<ul>
-
+ <li><%= link_to "Home", root_path %></li>
+ <li><%= link_to "Users", :userslist %></li>
<% if current_user %>
<li><%= link_to "Edit Profile", edit_user_path(current_user.id) %></li>
<li><%= link_to "Logout", :logout %></li>
- <li><%= link_to "my_downloads", :downloads %></li>
- <% else %>
+ <li><%= link_to "my_downloads", :my_downloads %></li>
+
+ <% else %>
<li><%= link_to "Register", new_user_path %></li>
<li><%= link_to "Login", :login %></li>
<% end %>
</ul>
- </nav>
+ </nav>
+
</header>
16 app/views/layouts/_style_and_meta.html.erb
View
@@ -0,0 +1,16 @@
+ <head>
+ <title>Social Network | <%= @title %></title>
+
+ <!--[if lt IE 9]>
+ <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
+ <![endif]-->
+ <%= stylesheet_link_tag 'blueprint/screen', :media => 'screen' %>
+ <%= stylesheet_link_tag 'blueprint/print', :media => 'print' %>
+ <!--[if lt IE 8]><%= stylesheet_link_tag 'blueprint/ie' %><![endif]-->
+ <%= stylesheet_link_tag 'custom', :media => 'screen' %>
+
+
+ <%= javascript_include_tag "application" %>
+ <%= csrf_meta_tags %>
+ </head>
+
42 app/views/layouts/application.html.erb
View
@@ -1,43 +1,13 @@
<!DOCTYPE html>
<html>
- <head>
- <title>Social Network | <%= @title %></title>
-
- <!--[if lt IE 9]>
- <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
- <![endif]-->
- <%= stylesheet_link_tag 'blueprint/screen', :media => 'screen' %>
- <%= stylesheet_link_tag 'blueprint/print', :media => 'print' %>
- <!--[if lt IE 8]><%= stylesheet_link_tag 'blueprint/ie' %><![endif]-->
- <%= stylesheet_link_tag 'custom', :media => 'screen' %>
+ <%= render 'layouts/style_and_meta' %>
- <%= javascript_include_tag "application" %>
- <%= csrf_meta_tags %>
- </head>
<body>
<div class="container">
- <header>
- <%= image_tag("logo.png", :alt => "Ruby School", :class => "round") %>
- <nav class="round">
- <ul>
- <li><%= link_to "Home", root_path %></li>
- <li><%= link_to "Users", :userslist %></li>
- <% if current_user %>
- <li><%= link_to "Edit Profile", edit_user_path(current_user.id) %></li>
- <li><%= link_to "Logout", :logout %></li>
- <li><%= link_to "downloads", :downloads %></li>
- <li><%= link_to "my_downloads", :my_downloads %></li>
-
- <% else %>
- <li><%= link_to "Register", new_user_path %></li>
- <li><%= link_to "Login", :login %></li>
- <% end %>
- </ul>
- </nav>
- </header>
-
+ <%= render 'layouts/header' %>
+
<div>
<p id="notice"><%= notice %></p>
<p id="alert"><%= alert %></p>
@@ -47,11 +17,5 @@
<%= yield %>
</section>
</div>
-
-<div>
-<!--<% if current_user %><% else %>
-<%= link_to "Register", new_user_path, :class => "signup_button round" %>
-<% end %>-->
-</div>
</body>
</html>
42 app/views/layouts/application.html.erb~
View
@@ -1,43 +1,13 @@
<!DOCTYPE html>
<html>
- <head>
- <title>Social Network | <%= @title %></title>
-
- <!--[if lt IE 9]>
- <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
- <![endif]-->
- <%= stylesheet_link_tag 'blueprint/screen', :media => 'screen' %>
- <%= stylesheet_link_tag 'blueprint/print', :media => 'print' %>
- <!--[if lt IE 8]><%= stylesheet_link_tag 'blueprint/ie' %><![endif]-->
- <%= stylesheet_link_tag 'custom', :media => 'screen' %>
+ <%= render 'layouts/head' %>
- <%= javascript_include_tag "application" %>
- <%= csrf_meta_tags %>
- </head>
<body>
<div class="container">
- <header>
- <%= image_tag("logo.png", :alt => "Ruby School", :class => "round") %>
- <nav class="round">
- <ul>
- <li><%= link_to "Home", :home %></li>
- <li><%= link_to "Users", :userslist %></li>
- <% if current_user %>
- <li><%= link_to "Edit Profile", edit_user_path(current_user.id) %></li>
- <li><%= link_to "Logout", :logout %></li>
- <li><%= link_to "downloads", :downloads %></li>
- <li><%= link_to "my_downloads", :my_downloads %></li>
-
- <% else %>
- <li><%= link_to "Register", new_user_path %></li>
- <li><%= link_to "Login", :login %></li>
- <% end %>
- </ul>
- </nav>
- </header>
-
+ <%= render 'layouts/header' %>
+
<div>
<p id="notice"><%= notice %></p>
<p id="alert"><%= alert %></p>
@@ -47,11 +17,5 @@
<%= yield %>
</section>
</div>
-
-<div>
-<!--<% if current_user %><% else %>
-<%= link_to "Register", new_user_path, :class => "signup_button round" %>
-<% end %>-->
-</div>
</body>
</html>
12 app/views/messages/create.html.erb
View
@@ -0,0 +1,12 @@
+<h1>Writing message to <%= @recipient.username %></h1>
+
+<%= form_for @message do |f| %>
+ <div class="field">
+ <%= f.label :content %><br />
+ <%= f.text_field :content %>
+ </div>
+ <div class="actions">
+ <%= f.submit "Submit" %>
+ </div>
+<% end %>
+<%= link_to 'Back', @recipient %>
7 app/views/messages/index.html.erb
View
@@ -0,0 +1,7 @@
+<% @messages.each do |message|%>
+ <div class="field">
+ <%= message.content %>
+ <%= message.user.username %>
+ </div>
+
+<% end %>
11 app/views/messages/show.html.erb
View
@@ -0,0 +1,11 @@
+<p>
+ <b>From:</b>
+ <%= @message.user.username %>
+</p>
+
+<p>
+ <b>Message:</b>
+ <%= @message.content %>
+</p>
+
+<%= link_to 'Back', :root %>
2  app/views/pages/home.html.erb
View
@@ -14,5 +14,3 @@
</tr>
</table>
- <h1>Social Network</h1>
- <%= link_to 'New User', new_user_path %>
4 app/views/posts/_post.html.erb
View
@@ -8,9 +8,7 @@
<% user = post.user rescue User.find(post.user_id) %>
<% if current_user?(user) %>
<td>
-<%= link_to "delete", post, :method => :delete,
-:confirm => "You sure?",
-:title => post.content %>
+<%= link_to "delete", post, :method => :delete,:confirm => "You sure?",:title => post.content %>
</td>
<% end %>
3  app/views/users/index.html.erb
View
@@ -15,8 +15,7 @@
<td><%= user.email %></td>
<td><%= link_to 'Show', user %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
- <td><%= link_to 'Destroy', user, confirm: 'Are you sure?', method: :delete %></td>
- </tr>
+ </tr>
<% end %>
</table>
8 app/views/users/show.html.erb
View
@@ -1,12 +1,12 @@
<table class="profile">
<tr>
<td class="sidebar round">
- <strong>Name</strong> <%= @user.username %><br />
+ <strong>Name</strong> <%= link_to @user.username, @user %><br />
<strong>E-mail</strong> <%= @user.email %><br />
- <strong>URL</strong> <%= link_to user_path(@user), @user %><br />
<strong>Posts</strong> <%= @user.posts.count %>
- <br>
- <%= link_to 'Edit', edit_user_path(@user) %> | <%= link_to 'Back', users_path %>
+ <%= link_to "Downloads", :downloads %>
+ <br>
+ <%= link_to 'Edit', edit_user_path(@user) %> | <%= link_to 'Back', users_path %> | <%= link_to 'write', send_message_path(@user) %>
</td>
</tr>
</table>
10 db/migrate/20120305154705_create_messages.rb
View
@@ -0,0 +1,10 @@
+class CreateMessages < ActiveRecord::Migration
+ def change
+ create_table :messages do |t|
+ t.integer :user_id
+ t.text :content
+
+ t.timestamps
+ end
+ end
+end
10 db/migrate/20120305154753_create_messages_recipients.rb
View
@@ -0,0 +1,10 @@
+class CreateMessagesRecipients < ActiveRecord::Migration
+ def change
+ create_table :message_recipients do |t|
+ t.integer :user_id
+ t.integer :message_id
+
+ t.timestamps
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.