通过jquery使用集合更新rails部分
Updating rails partial with a collection through jquery
我有一个允许用户发布更新的表单。一旦用户发布更新,我希望更新列表刷新。为了实现这一点,我使用Ajax和jQuery,并使用Rails。我遇到了麻烦,而试图让jquery渲染post feed部分虽然。
这是我使用的jquery
$(".microposts").html("<%= j render partial: 'shared/feed_item', collection: @feed_items %>")
目前,提要只是刷新并且不显示任何内容。我相信这是由于我试图传递@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
@feed_items
需要在控制器中的某处定义。@
是Ruby中的一个特殊符号,表示当前类的实例变量。如果你在别处定义了它,它就变成了那个类的实例变量。
Rails有一些特殊的魔力,使控制器的实例变量在视图中可用。如果它不是控制器上的实例变量,它将无法工作。
def create
@micropost = current_user.microposts.build(micropost_params)
if @micropost.save
@feed_items = @micropost.do_whatever_to_build_the_feed_items
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
相关文章:
- 在Rails中更新Div,而不更改更新请求后的视图
- 更新'喜欢'使用Ajax的Rails中的计数
- 使用rails中的ajax请求更新DB
- Ruby on rails-更新ajax的PUT方法
- Heroku Ruby on Rails应用程序don't更新公共/资产目录中的未消化资产
- 带有i18njs-gem的Rails 4 translations.js没有更新
- Rails使用ajax调用更新多态对象的表部分
- Rails和jQuery-尝试使用setTimeout主动更新数据
- 如何使用Ajax更新Rails中的Button
- 定期轮询数据库,Rails 视图更新而不刷新
- Ajax 调用不更新 Rails 上的内容
- 更新的 HTML 表单无法被 Rails 提交识别
- Rails/Ajax 在提交后将表单从创建更改为更新
- 通过AJAX更新Rails模型
- 使用Ajax更新Rails中的元素
- 从JS事件中更新Rails模型
- 如何更新rails中Ajax删除的记录
- 使用ajax更新Rails
- 通过jquery使用集合更新rails部分
- 充当Votable Ajax投票大小不更新Rails