2013-02-20 56 views
0

我通過集合選項將一個集合(@feed_items)傳遞給_feed_item部分,並將其轉換爲dailypost:as =>:dailypost。Like按鈕,用戶的dailyposts不與AJAX更新

在_feed_item部分內部,我爲_like_button渲染了另一個部分,並且我使用了:locals繼續使用dailypost。

一切工作正常與數據庫。喜歡加入和取出:)但

我想通過toggle.js.erb使用(AJAX)喜歡或不同。 出於某種原因,它不能正確更新,我必須每次刷新頁面。有什麼建議麼???

模型

class Like < ActiveRecord::Base 
    attr_accessible :dailypost_id, :user_id 
    belongs_to :user 
    belongs_to :dailypost 

    default_scope order: 'likes.created_at DESC' 
end 

控制器

likes_controller.rb 

class LikesController < ApplicationController 

    respond_to :html, :js 

def create 
    @like = Like.create(params[:like]) 
    @dailypost = @like.dailypost 
    render :toggle 
end 

def destroy 
    like = Like.find(params[:id]).destroy 
    @dailypost = like.dailypost 
    puts "----------------like destroyed" 
    render :toggle 
end 

end 

查看

_feed.html.erb

<% if @feed_items.any? %> 
    <ol class="dailyposts postusers"> 
    <%= render partial: 'shared/feed_item', collection: @feed_items, :as => :dailypost %> 
    </ol> 
    <%= will_paginate @feed_items %> 
<% end %> 

_feed_items.html.erb

<li id="<%= dailypost.id %>"> 

    <span class="user"> 
    <%= link_to dailypost.user.name, dailypost.user %> 
    <%= link_to dailypost.id %> 
    <span class="content"><%= dailypost.content_html %></span> 

    <div id="like"> 
    <%= render :partial => 'likes/like_button', :locals =>{:dailypost => dailypost} %> 
    </div> 

</li> 

_like_button.html.erb

<% if like = current_user.likes.find_by_dailypost_id(dailypost.id) %> 
<%= form_for like, :html => { :method => :delete }, :remote => true do |f| %> 
    <%= f.submit "Unlike" %> 
<% end %> 
<% else %> 
<%= form_for current_user.likes.build, :remote => true do |f| %> 
    <div><%= f.hidden_field :dailypost_id, value: dailypost.id %></div> 
    <%= f.hidden_field :user_id %> 
    <%= f.submit "Like" %> 
<% end %> 
<% end %> 

toggle.js.erb

$("#like").html("<%= escape_javascript(render :partial => 'like_button', :locals => {:dailypost => @dailypost}) %>"); 

DATABASE

ActiveRecord::Schema.define(:version => 20130210095553) do 

create_table "dailyposts", :force => true do |t| 
    t.string "content" 
    t.string "content_html" 
    t.integer "user_id" 
    t.datetime "created_at",   :null => false 
    t.datetime "updated_at",   :null => false 
end 

add_index "dailyposts", ["user_id", "created_at"], :name => "index_dailyposts_on_user_id_and_created_at" 

create_table "likes", :force => true do |t| 
    t.integer "dailypost_id" 
    t.integer "user_id" 
    t.datetime "created_at", :null => false 
    t.datetime "updated_at", :null => false 
end 

create_table "users", :force => true do |t| 
    t.string "name" 
    t.string "email" 
    t.datetime "created_at",        :null => false 
    t.datetime "updated_at",        :null => false 
    t.string "password_digest" 
    t.string "remember_token" 
    t.boolean "admin",     :default => false 
end 

add_index "users", ["email"], :name => "index_users_on_email", :unique => true 
add_index "users", ["remember_token"], :name => "index_users_on_remember_token" 

end 

回答

1

看起來像你的表格是不是正確設置dailypost_id,請嘗試更改您的形式:

<%= form_for(current_user.likes.build(dailypost_id: @dailypost.id), remote: true) do |f| %> 
    <%= f.hidden_field :dailypost_id %> 

到:

<%= form_for(current_user.likes.build, remote: true) do |f| %> 
    <%= f.hidden_field :dailypost_id, value: @dailypost.id %> 
+0

我仍然得到dailypost爲NIL,所以我的錯誤是在我的形式,而不是在我喜歡的控制器?我是新來的鐵路,所以任何幫助,非常感謝 – 2013-02-20 22:21:33

+0

@SurgePedroza在仔細觀察,你實際上並沒有'@ dailypost'定義的任何地方......只有'@ dailyposts'。你確定你不是故意使用'dailypost'而不是'@ dailypost'嗎?我假設你正在通過'@ dailyposts'在你認爲你沒有顯示的情況下進行某種循環? – Noz 2013-02-20 22:47:13

+0

現在一切正常,我只是有一個Ajax問題。你能幫我嗎:) – 2013-02-24 02:43:08