像按钮不刷新到不同,但关系是在模型中创建的

时间:2011-04-22 13:02:44

标签: ruby-on-rails ruby-on-rails-3 entity-relationship

您好我设置了一套感谢控制器来处理用户喜欢的不同帖子。每个帖子都有一个类似的按钮,当我点击它看起来像请求通过,但页面不刷新和更新按钮。

当我点击这样是日志时,它会显示返回的不同部分,但没有任何变化:

Started POST "/appreciations" for 127.0.0.1 at 2011-04-22 05:47:28 -0700
Processing by AppreciationsController#create as JS
Parameters: {"utf8"=>"✓",   "authenticity_token"=>"zQQJeXZiAPFeQ/7AEy9hvQac01+jq929XUXHrd6eSOE=",    "appreciation"=>{"liked_id"=>"3"}, "commit"=>"Like"}
User Load (1.1ms)  SELECT "users".* FROM "users" WHERE ("users"."id" = 4) LIMIT 1
Post Load (0.4ms)  SELECT "posts".* FROM "posts" WHERE ("posts"."id" = 3) ORDER BY posts.created_at DESC LIMIT 1
SQL (0.5ms)  INSERT INTO "appreciations" ("created_at", "liked_id", "liker_id", "updated_at") VALUES ('2011-04-22 12:47:28.642264', 3, 4, '2011-04-22 12:47:28.642264')
Redirected to http://localhost:3000/posts/3
Completed 302 Found in 185ms

Started GET "/posts/3" for 127.0.0.1 at 2011-04-22 05:47:28 -0700
Processing by PostsController#show as HTML
Parameters: {"id"=>"3"}
Post Load (0.4ms)  SELECT "posts".* FROM "posts" WHERE ("posts"."id" = 3) ORDER BY posts.created_at DESC LIMIT 1
User Load (1.2ms)  SELECT "users".* FROM "users" WHERE ("users"."id" = 4) LIMIT 1
Appreciation Load (0.3ms)  SELECT "appreciations".* FROM "appreciations" WHERE ("appreciations".liker_id = 4) AND ("appreciations"."liked_id" = 3) LIMIT 1
CACHE (0.0ms)  SELECT "appreciations".* FROM "appreciations" WHERE ("appreciations".liker_id = 4) AND ("appreciations"."liked_id" = 3) LIMIT 1
Rendered posts/_unlike.html.erb (49.4ms)
Rendered users/_like_form.html.erb (77.7ms)
Rendered posts/show.html.erb within layouts/application (208.9ms)
Completed 200 OK in 248ms (Views: 212.4ms | ActiveRecord: 5.2m

赞赏控制器

class AppreciationsController < ApplicationController
  before_filter :authenticate_user!

  def create
    @post = Post.find(params[:appreciation][:liked_id])
    current_user.like!(@post)
    redirect_to @post
  end

  def destroy
    @post = Appreciation.find(params[:id]).liked
    current_user.unlike!(@post)
    redirect_to @post
  end

end

_like_form.html.erb

<% unless current_user?(@user) %>
  <div id="like_form">
    <% if current_user.likes?(@post) %>
    <%= render 'posts/unlike' %>
  <% else %>
    <%= render 'posts/like' %>
  <% end %>
 </div>

&lt;%end%&gt;

_like.html.erb

<%= form_for(current_user.appreciations.
                      build(:liked_id => @post.id),
                      :remote => true) do |f| %>
 <div><%= f.hidden_field :liked_id %></div>
 <div class="actions"><%= f.submit "Like" %></div>
<% end %>

_unlike.html.erb

<%= form_for(current_user.appreciations.find_by_liked_id(@post),
                               :html => { :method => :delete },
                               :remote => true) do |f| %>
<div class="actions"><%= f.submit "Unlike" %></div>
<% end %>

1 个答案:

答案 0 :(得分:0)

POST“/赞赏”的结果是否应重定向到http://localhost:3000/posts/3?你正在使用:remote =&gt;表单的真实选项,所以我认为你需要将控制器更改为:

respond_to do |format|
  format.html { redirect_to @post}
  format.js
end

并创建一个* .js.erb partial,它可以执行以下操作:

<% unless current_user?(@user) %>
  <% if current_user.likes?(@post) %>
    $("#post_form").html("<%= escape_javascript(render('posts/unlike'))%>");>
  <% else %>
    $("#post_form").html("<%= escape_javascript(render('posts/like'))%>");
  <% end %>
<% end %>  

基本上我相信你没有看到任何变化,因为你正在进行AJAX POST,但没有对结果做任何事情。您需要使用您的帖子结果更新DOM元素的innerHTML的JavaScript。

这篇文章可能会有所帮助:http://www.stjhimy.com/posts/7-creating-a-100-ajax-crud-using-rails-3-and-unobtrusive-javascript