Last active
December 14, 2015 20:29
-
-
Save JustSilverman/5144365 to your computer and use it in GitHub Desktop.
Rails Ajax Snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<li><%= post.content %></li> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('ul.posts').append("<%= escape_javascript(render 'post_row', :post => @post) %>"); | |
$('form input[type="text"]').val(""); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= 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 %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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