Skip to content

Instantly share code, notes, and snippets.

@dhh
Last active December 30, 2016 23:57
Show Gist options
  • Save dhh/7901105 to your computer and use it in GitHub Desktop.
Save dhh/7901105 to your computer and use it in GitHub Desktop.
  1. First-use of the message template
<% renders messages/_message.html.erb %>
<%= render @messages %>
  1. Form submitting via Ajax
<%= form_for @project.messages.new, remote: true do |form| %>
  ...
  <%= form.submit "Send message" %>
<% end %>
  1. Server creates the model object
class MessagesController < ActionController::Base
  def create
    @post = @project.messages.create!(message_params)

    respond_to do |format|
      format.html { redirect_to @message } # no js fallback
      format.js   # just renders messages/show.js.erb
    end
  end
end
  1. Server generates a JavaScript view
<%# renders messages/_message.html.erb %>
$('#messages').prepend('<%=j render @message %>');
$('#<%= dom_id @message %>').highlight();
  1. Automatic evaluation of response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment