【问题标题】:controller does not respond to Ajax控制器不响应 Ajax
【发布时间】:2017-07-14 09:46:46
【问题描述】:

我想使用 Ajax 将微博添加到我的主页,而无需在创建后立即重定向。我在表格中添加了remote: true

<%= form_for(@micropost, html: { multipart: true }, remote: true) do |f| %>

并编辑 microposts 控制器的创建动作如下:

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

微博在首页显示为有序列表,其中@feed_itemscurrent_user的微博集合,属于微博:

<% if @feed_items.any? %>
  <ol class="microposts">
    <%= render @feed_items %>
  </ol>
  <%= will_paginate @feed_items %>
<% end %>

因此我创建了app/views/microposts/create.js.erb,使用jQuery选择ol.microposts和函数prepend()将新创建的微博添加到页面:

$("ol.microposts").prepend('<%= escape_javascript(render partial: @micropost) %>');

用于在ol.microposts 中构建li 元素的部分_micropost.html.erb(简化)如下:

<li id="micropost-<%= micropost.id %>">
  <%= link_to gravatar_for(micropost.user, size: 50), micropost.user %>
  <span class="user"><%= link_to micropost.user.name, micropost.user %></span>
  <span class="content">
    <%= micropost.content %>
    <%= image_tag micropost.picture.url if micropost.picture? %>
  </span>
</li>

然而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

不明白为什么微博控制器的创建动作不响应js格式。我试图克隆部分_micropost.html.erb并使用实例变量@micropost而不是迭代的变量micropost,但它不起作用。服务器日志中没有错误。

【问题讨论】:

  • 首先你应该响应每个if/else-path到js。
  • 提交时日志显示什么?我相信请求格式是HTML
  • 我在原始消息的末尾复制并粘贴了服务器日志。在大约 5 秒的初始等待后,它开始到 get /,这就是 respond_to html 所说的。
  • 不,不是这样。我需要在您提交表单后立即打印的日志
  • 我更新了我原来的消息:Processing by MicropostsController#create as HTML

标签: ruby-on-rails ajax


【解决方案1】:

MicropostsController 处理#create as HTML

这是由于AJAX限制。来自Reference

Ajax 使用称为xmlhttprequest 的东西来发送您的数据。 不幸的是,xmlhttprequests 不能发布文件

也就是说,您不能通过 AJAX 发布文件。您可能需要RemotipartJquery-File-Upload 的帮助

【讨论】:

    【解决方案2】:

    您只在成功的保存路径中将操作告诉respond_toformat.js。您需要在 renderredirect_to 的任何地方添加一个 respond_to 块,其中包含您想为您的 js 做的任何事情。

    【讨论】:

    • 我只想回复js if @micropost.savehtml 在所有其他情况下。
    猜你喜欢
    • 2020-08-06
    • 1970-01-01
    • 2021-12-09
    • 1970-01-01
    • 2019-08-13
    • 2013-11-29
    • 2014-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多