Skip to content

Commit bfdf26e

Browse files
committed
使用ajax实现评论局部刷新
1 parent 2e77753 commit bfdf26e

File tree

8 files changed

+38
-22
lines changed

8 files changed

+38
-22
lines changed

app/assets/stylesheets/application.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,9 @@
1313

1414
.favorite_color {
1515
color: green;
16+
}
17+
18+
.badge_color {
19+
color: white;
20+
background-color: #269abc;
1621
}

app/controllers/comments_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ def edit
1212
def create
1313
@article = Article.find(params[:article_id])
1414
@comment = @article.comments.create!(comment_params.merge(name: current_user.user_name))
15-
redirect_to user_article_path(@article.user, @article), :notice => "Comment created!"
16-
# render :partial => "show"
15+
# redirect_to user_article_path(@article.user, @article), :notice => "Comment created!"
16+
render :show
1717
end
1818

1919
def update
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<% if current_user == @article.user %>
2+
<%= link_to edit_user_article_path(@user, @article) do %>
3+
<span class="glyphicon glyphicon-pencil"></span>
4+
<% end %> |
5+
<%= link_to [@user, @article], method: :delete, data: { confirm: 'Are you sure?' } do %>
6+
<span class="glyphicon glyphicon-trash"></span>
7+
<% end %>
8+
<% end %>

app/views/articles/home.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
<% if article.comments.count.to_i != 0 %>
1818
<div class="media-right">
19-
<span class="badge"><%= article.comments.count.to_i %></span>
19+
<span class="badge badge_color"><%= article.comments.count.to_i %></span>
2020
</div>
2121
<% end %>
2222
</div>

app/views/articles/show.html.erb

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,32 @@
66
<div class="panel-heading">
77
<h4><p class="text-center"><%= @article.title %></p></h4>
88
</div>
9+
910
<div class="panel-body">
1011
<p><%= markdown(@article.content) %></p>
1112
</div>
13+
1214
<div class="panel-footer" >
13-
<!-- <div class="container"> -->
1415
<div class="row">
15-
<div id="favorite" class="col-md-6 text-left"><%= render 'favorite_link' %></div>
16-
<div class="col-md-6 text-right">
17-
<% if current_user == @article.user %>
18-
<%= link_to edit_user_article_path(@user, @article) do %>
19-
<span class="glyphicon glyphicon-pencil"></span>
20-
<% end %> |
21-
<%= link_to [@user, @article], method: :delete, data: { confirm: 'Are you sure?' } do %>
22-
<span class="glyphicon glyphicon-trash"></span>
23-
<% end %>
24-
<% end %>
25-
</div>
26-
<!-- </div> -->
27-
</div>
16+
17+
<div id="favorite" class="col-md-6 text-left">
18+
<%= render 'favorite_link' %>
19+
</div>
20+
21+
<div class="col-md-6 text-right">
22+
<%= render 'edit_delete' %>
23+
</div>
24+
25+
</div>
2826
</div>
27+
2928
</div>
3029

31-
<% if @article.comments.size > 0 %>
32-
<%= render 'comments/show', object: @article %>
33-
<% end %>
30+
<div id="comment">
31+
<% if @article.comments.size > 0 %>
32+
<%= render 'comments/show', object: @article %>
33+
<% end %>
34+
</div>
3435

3536
<%= render 'comments/form' %>
3637

app/views/comments/_form.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<%= form_for([@article, @comment = Comment.new], html: {class: 'form-horizontal'}) do |f| %>
1+
<%= form_for([@article, @comment = Comment.new], remote: true, html: {class: 'form-horizontal'}) do |f| %>
22
<% if @comment.errors.any? %>
33
<div id="error_explanation">
44
<h2><%= pluralize(@comment.errors.count, "error") %> prohibited this comment from being saved:</h2>

app/views/comments/show.js.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
$("#comment").html("<%= escape_javascript(render partial: 'show') %>");
2+
// $("#my_articles").html("");

app/views/users/show.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<div class="row">
22
<ul class="list-group col-md-3">
33
<li class="list-group-item">
4-
<p class="text-left"><%= image_tag avatar_url(@user, 100) %></p>
4+
<p class="text-left"><%= image_tag avatar_url(@user, 150) %></p>
55
<p><h3><%= @user.user_name %></h3></p>
66
</li>
77
<li class="list-group-item">

0 commit comments

Comments
 (0)