我有一個允許用戶發佈更新的表單。一旦用戶發佈更新,我希望刷新更新列表。爲了實現這一點,我使用Ajax和jQuery,並使用Rails。雖然試圖讓jquery渲染後饋部分,但我遇到了麻煩。通過jquery更新導軌局部集合
下面是我使用
$(".microposts").html("<%= j render partial: 'shared/feed_item', collection: @feed_items %>")
目前jQuery的,飼料只是刷新顯示任何內容。我相信這是因爲我試圖通過@feed_items。傳遞該變量的最佳方式是什麼?
有人問控制器;
class MicropostsController < ApplicationController
before_action :signed_in_user, only: [:create, :destroy]
before_action :correct_user, only: :destroy
def create
@micropost = current_user.microposts.build(micropost_params)
if @micropost.save
respond_to do |format|
format.html { redirect_to root_url }
format.js
end
else
@feed_items = []
flash[:error] = "Failed to create micropost!"
render 'static_pages/home'
end
end
def destroy
@micropost.destroy
flash[:success] = "Micropost deleted!"
redirect_to root_url
end
private
def micropost_params
params.require(:micropost).permit(:content)
end
def correct_user
@micropost = current_user.microposts.find_by(id: params[:id])
redirect_to root_url if @micropost.nil?
end
end
你也可以粘貼控制器嗎? – matanco
@matanco在我添加,我知道控制器的工作作爲其他元素更新時,發佈。 – Adam
'@ feed_items'無論如何都是空的。也許這會導致你的問題。我沒有看到任何情況下你把東西放入這個變量。 – matanco