作者:knight | 来源:互联网 | 2023-09-14 16:48
IwouldliketouseAjaxtoaddamicroposttomyhomepagewithoutredirectassoonasitiscreate
I would like to use Ajax to add a micropost to my home page without redirect as soon as it is created. I added remote: true
in the form:
我希望使用Ajax向我的主页添加微博,而不需要在创建后立即重定向。我在表格中添加了remote: true:
<%= form_for(@micropost, html: { multipart: true }, remote: true) do |f| %>
and edited the create action of the microposts controller as follows:
并对微贴子控制器的创建动作进行如下编辑:
def create
@micropost = current_user.microposts.build(micropost_params)
microposts_number = current_user.microposts.where("created_at >= ?", Time.zone.now.beginning_of_day).count
if microposts_number <10
if @micropost.save
respond_to do |format|
format.html do
flash[:success] = "Micropost created!"
redirect_to root_url
end
format.js
end
else
@feed_items = []
flash[:danger] = @micropost.errors.full_messages.join(', ')
render 'static_pages/home'
end
else
flash[:danger] = "You have exceeded your daily share of microposts (10)."
redirect_to root_url
end
end
The microposts are shown in the home page as orded list of items, where @feed_items
is a collection of microposts for current_user
, thus belonging to Micropost:
微博在主页上显示为项目列表,其中@feed_items是current_user微博的集合,属于微博:
<% if @feed_items.any? %>
<%= render @feed_items %>
<%= will_paginate @feed_items %>
<% end %>
Therefore I created app/views/microposts/create.js.erb
, using jQuery to select ol.microposts
and function prepend()
to add the newly created micropost to the page:
所以我创建了app /视图/ microposts / create.js。erb,使用jQuery选择ol。微贴子和函数prepend()将新创建的微贴子添加到页面:
$("ol.microposts").prepend('<%= escape_Javascript(render partial: @micropost) %>');
The partial _micropost.html.erb
, used to build the li
elements inside ol.microposts
is (simplified) below:
分_micropost.html。erb,用于在ol内构建li元素。microposts如下(简体):
<%= link_to gravatar_for(micropost.user, size: 50), micropost.user %>
<%= link_to micropost.user.name, micropost.user %>
<%= micropost.content %>
<%= image_tag micropost.picture.url if micropost.picture? %>
However the Micropost controller does not respond to the Ajax request but redirect to root_url
, responding only to html (output from cloud9 server):
但是,Micropost控制器不响应Ajax请求,而是重定向到root_url,只响应html (cloud9服务器的输出):
Started POST "/microposts" for 82.56.61.198 at 2017-07-14 09:44:55 +0000
Cannot render console from 82.56.61.198! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by MicropostsController#create as HTML
...
Started GET "/" for 82.56.61.198 at 2017-07-14 08:51:42 +0000
Cannot render console from 82.56.61.198! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by StaticPagesController#home as HTML
I do not understand why the create action of the Micropost controller does not respond to js format. I tried to clone partial _micropost.html.erb
and use the instance variable @micropost
instead of the variable micropost
of the iteration, but it did not work. There are no errors in the server log.
我不明白为什么微邮报控制器的创建操作不响应js格式。我尝试克隆部分_micropost.html。erb并使用实例变量@micropost代替迭代的变量micropost,但它不起作用。服务器日志中没有错误。
2 个解决方案