Skip to content

Instantly share code, notes, and snippets.

@chikamichi
Created December 22, 2009 02:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chikamichi/261444 to your computer and use it in GitHub Desktop.
Save chikamichi/261444 to your computer and use it in GitHub Desktop.
// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
jQuery.ajaxSetup({
'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
});
jQuery.fn.submitWithAjax = function () {
this.submit(function () {
// $.post($(this).attr('action'), $(this).serialize(), null, "script");
$.ajax({
type: $(this).attr('method'),
url: $(this).attr('action'),
data: $(this).serialize(),
dataType: "script"
});
return false;
});
};
$(document).ready(function() {
$("#chat_room_form").submitWithAjax();
})
class ChatRoomsController < ApplicationController
# ... all the action generated by script/generate: index, new, show, edit, destroy
def send_data
render :juggernaut => {:type => :send_to_channels,
:channels => [params[:channel_id].to_i]} do |page|
page["#chat_room"].prepend "<li>#{h params[:chat_input]}</li>"
end
#render :nothing => true
end
end
- use respond_to (or something else?) in send_data to externalize the .js processing, in the same fashion one would use a .rjs file.
- be able to use :method => "get" without triggering the show action.
$("#chat_room").append('<li><%= escape_javascript("#{params[:chat_input]}") %></li>').fadeIn('fast').highlight;
<%= javascript_include_tag 'jquery', 'json', 'juggernaut/juggernaut', 'juggernaut/jquerynaut', 'juggernaut/swfobject', 'application' %>
<%= juggernaut(:debug => false, :channels => [@chat_room.id]) %>
(id: <%= @chat_room.id %>)
<p><%= link_to 'Back', chat_rooms_path %></p>
<h1><%=h @chat_room.name %></h1>
<p><%=h @chat_room.description %></p>
<% form_tag({:action => "send_data"}, :method => "post", :id => "chat_room_form") do %>
<%= text_field_tag(:chat_input) %>
<%= hidden_field_tag(:channel_id, @chat_room.id) %>
<%= submit_tag("Talk (JQuery with form_tag)") %>
<% end %>
<ul id="chat_room" style="list-style: none"></ul>
<%= link_to 'Edit', edit_chat_room_path(@chat_room) %> |
<%= link_to 'Back', chat_rooms_path %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment