“喜欢”按钮不会刷新为“喜欢”,但关系是在模型中创建的

发布于 2024-11-03 04:12:44 字数 3146 浏览 5 评论 0原文

您好,我已经设置了一组欣赏控制器来处理用户对不同帖子的喜欢。每个帖子都有一个“赞”按钮,当我单击它时,看起来请求已通过,但页面不会刷新和更新按钮。

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

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

apprementscontroller_like_form.html.erb

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

<

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

% end %>;

_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 %>

Hi I've set set of an appreciation controller that handles user's liking different posts. Each post has a like button and when I click it looks like the request goes through but the page doesn't refresh and update the button.

When I click like this is the log, it shows the unlike partial being returned but nothing changes:

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

appreciations controller

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>

<% end %>

_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 %>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧话新听 2024-11-10 04:12:44

POST "/appreciations" 的结果是否应该重定向到 http://localhost:3000/posts/3?您正在使用 :remote =>; true 选项的形式,所以我相信你需要将你的控制器更改为:

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

并创建一个 *.js.erb 部分,执行如下操作:

<% 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,但没有对结果做任何事情。您需要 JavaScript 来使用您的帖子结果更新 DOM 元素的innerHTML。

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

Should the result of POST "/appreciations" be a redirect to http://localhost:3000/posts/3? You're using the :remote => true option of the form so I believe you need to change your controller to this:

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

And create a *.js.erb partial that does something like this:

<% 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 %>  

Basically I believe you're seeing no change because you're making an AJAX POST, but not doing anything with the result. You would need JavaScript that updates the innerHTML of a DOM element with the result of your post.

This post might be helpful: http://www.stjhimy.com/posts/7-creating-a-100-ajax-crud-using-rails-3-and-unobtrusive-javascript

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文