Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

AJAX Examples

  • Loading branch information...
commit cfed11ba1f9bf28eca6ed1064079c1f1cd31dde6 1 parent a2474a5
@newfront authored
View
15 week_five/faux-twitter/app/controllers/main_controller.rb
@@ -1,4 +1,5 @@
class MainController < ApplicationController
+ #layout 'cool'
def index
# Welcome Screen
@@ -8,5 +9,19 @@ def index
# If notices, let the user know
# flash[:notice]
end
+
+ def find_me_info
+ # add information into the page dynamically at run time
+ # the_info goes into the partial
+ the_info = params[:what_info] if params[:what_info].is_a? String
+
+ @status = UserStatus.find(the_info)
+ #render :text => "Last Tweet : #{@status.status_update}"
+
+ # the partial is rendered out using the local variable from the parameters params[:what_info]
+ render :text => @status.status_update
+ #render :partial => '/partials/add_content_now', :locals =>{:my_info => the_info}
+
+ end
end
View
2  week_five/faux-twitter/app/controllers/session_controller.rb
@@ -15,7 +15,7 @@ def create
#"user"=>{"username"=>"matt", "password"=>"[FILTERED]"}, "commit"=>"Log In"}
if params[:commit] == "Log In"
- @current = u = User.find_by_username_and_password(params[:user][:username],params[:user][:password])
+ @current = User.find_by_username_and_password(params[:user][:username],params[:user][:password])
if @current.is_a? User
# Start Saving Session Info for the User
puts params[:user].inspect
View
3  week_five/faux-twitter/app/views/layouts/application.html.erb
@@ -3,12 +3,13 @@
<head>
<title><%= @page_title %></title>
<%= stylesheet_link_tag :all, "main_style","forms","buttons","errors","users", "statuses"%>
+
<%= javascript_include_tag "jquery","application"%>
<%= csrf_meta_tag %>
</head>
<body>
- <%= render :partial => '/partials/main_header'%>
+ <%= render '/partials/main_header'%>
<div id="mainwrap">
<%= yield %>
View
22 week_five/faux-twitter/app/views/layouts/cool.html.erb
@@ -0,0 +1,22 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <title><%= @page_title %></title>
+ <%= stylesheet_link_tag :all, "main_style","forms","buttons","errors","users", "statuses"%>
+
+ <%= javascript_include_tag "jquery","application"%>
+
+ <%= csrf_meta_tag %>
+</head>
+<body>
+ <%= render '/partials/main_header'%>
+ <div id="mainwrap">
+
+ <%= yield %>
+
+ </div>
+ <div class="clearfix"></div>
+ <div id="footer">
+ </div>
+</body>
+</html>
View
5 week_five/faux-twitter/app/views/main/index.html.erb
@@ -4,5 +4,8 @@
</div>
<% end %>
<div id="content">
- <%= render :partial => 'partials/user_status', :locals => {:userid => 'all'}%>
+ <%= render 'partials/user_status', :locals => {:userid => 'all'}%>
+ <div id="dynamic_information_div"></div>
+ <a href="" id="event_button" >EVENT BUTTON</a>
+
</div>
View
10 week_five/faux-twitter/app/views/partials/_add_content_now.html.erb
@@ -0,0 +1,10 @@
+<%
+%>
+<div id="add_content_now">
+ <p><%= my_info %></p>
+ <ul>
+ <li>ONE</li>
+ <li>TWO</li>
+ <li>THREE</li>
+ </ul>
+</div>
View
17 week_five/faux-twitter/app/views/partials/_user_status.html.erb
@@ -1,13 +1,14 @@
<%
#userid
# Get user with id of 4, find all statuses
- if userid != "all"
- @u = User.find(userid.to_i)
- @type = "single"
- elsif userid.is_a? String
- @s = UserStatus.where(:user_id != 0)
- @type = "all"
- end
+ # unless userid == "all"
+ #if userid != "all"
+ # @u = User.find(userid.to_i)
+ # @type = "single"
+ #elsif userid.is_a? String
+ # @s = UserStatus.where(:user_id != 0)
+ # @type = "all"
+ #end
%>
<% if @u.is_a? User %>
<% @u.user_statuses.each do |status| %>
@@ -19,7 +20,7 @@
By <%= link_to @u.username ,user_path(@u.id),:class => 'user_highlight'%> <%= status.created_at.strftime("%b %d %Y %I:%M:%S")%></div>
</div>
<% end %>
-<% elsif @type = "all"%>
+<% elsif @type == "all"%>
<% @s.each do |status| %>
<div class="status_update">
<div class="user_status">
View
2  week_five/faux-twitter/config/routes.rb
@@ -1,6 +1,8 @@
FauxTwitter::Application.routes.draw do
get "main/index"
+
+ get "main/find_me_info"
get "user_preference/show"
View
28 week_five/faux-twitter/public/javascripts/application.js
@@ -1 +1,27 @@
-/* Main Application Functions */
+/* Main Application Functions */
+
+$(document).ready(function()
+{
+
+ $("#event_button").click(function()
+ {
+ //alert("we clicked the button");
+
+ // make an AJAX call to /main/find_me_info,
+ // alert findings
+
+ $.ajax({
+ type : "GET",
+ url : "/main/find_me_info",
+ data : "what_info=6",
+ success: function(msg)
+ {
+ //alert(msg);
+ $("#dynamic_information_div").html("<div class='cool_div'><p>"+msg+"</p></div>");
+ }
+ });
+
+ return false;
+ });
+
+});
View
10 week_five/faux-twitter/public/stylesheets/statuses.css
@@ -19,4 +19,14 @@
color:#999999;
display:block;
margin-left:10px;
+}
+
+.cool_div
+{
+ display:block;
+ background:#ffffff;
+ padding:10px;
+ border:1px solid #cccccc;
+ font-size:15px;
+ color:#990000;
}
Please sign in to comment.
Something went wrong with that request. Please try again.