Skip to content

Instantly share code, notes, and snippets.

@JustSilverman
Last active December 14, 2015 20:29
Show Gist options
  • Save JustSilverman/5144365 to your computer and use it in GitHub Desktop.
Save JustSilverman/5144365 to your computer and use it in GitHub Desktop.
Rails Ajax Snippets
<li><%= post.content %></li>
$('ul.posts').append("<%= escape_javascript(render 'post_row', :post => @post) %>");
$('form input[type="text"]').val("");
<%= form_for(@post, :remote => true) do |f| %>
<div class="field">
<%= f.label :content %><br />
<%= f.text_field :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<% if @posts %>
<ul class="posts">
<% @posts.each do |post| %>
<%= render 'post_row', :post => post %>
<% end %>
</ul>
<% end %>
def create
@post = Post.new(params[:post])
respond_to do |format|
if @post.save
format.html { redirect_to @post }
format.js
else
format.html { render "new" }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment