加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – ajax:完全火灾但是ajax:成功不在rails 3应用

发布时间:2020-12-16 21:49:13 所属栏目:百科 来源:网络整理
导读:我是RoR的新手,但是为一个小应用程序实现各种功能而取得了成功.直到我遇到这个问题,没有发现任何现有的问题/问题.为了帮助我将问题缩小到通用形式,这里是我所拥有的: 用户故事 用户被呈现为创建主题(名称和描述)的表单,一旦创建了主题,用户被呈现为允许用户
我是RoR的新手,但是为一个小应用程序实现各种功能而取得了成功.直到我遇到这个问题,没有发现任何现有的问题/问题.为了帮助我将问题缩小到通用形式,这里是我所拥有的:

用户故事

用户被呈现为创建主题(名称和描述)的表单,一旦创建了主题,用户被呈现为允许用户添加子主题的“显示”页面.当用户添加子主题时,它们会在同一页面上显示给用户(这是我试图使用ajax的地方).

代码人工制品

型号==> topic_request.rb

class TopicRequest < ActiveRecord::Base
 has_many   :subtopics,:class_name=>"TopicRequest",:foreign_key=>"parent_id"
end

控制器==> topic_requests_controller.rb

class TopicRequestsController < ApplicationController
  expose(:user_topic_requests) {current_user.topic_requests}
  expose(:topic_request)
  expose(:sub_topic_request) {TopicRequest.new}

  def new
  end

  def create
    topic_request.save
    if (topic_request.parent_id != nil)
    parentreq = TopicRequest.find(topic_request.parent_id)
    render :partial => 'subreqs',
           :locals => {:topic_requests => parentreq.subtopics},
           :layout => false
    else
    render :show
    end
  end

  def show
  end

  def index
  end
end

new.html.slim

= simple_form_for topic_request,:html => {:class => 'form-stacked'} do |f|
  = f.error_notification
  fieldset
    = f.input :name,:required => true
    = f.input :description,:required => true
  .actions
    = f.button :submit,"Propose Topic"

show.html.slim

# display the parent topic
= topic_request.name
= topic_request.description

#display the form for accepting subtopic requests
= simple_form_for sub_topic_request,
                  :url => {:controller => "topic_requests",:action => "create"},
                  :remote => true,
                  :html => {:class => 'form-stacked',
                            :id => 'new_subtopic_request'} do |f|
  = f.error_notification
  fieldset
    = f.input :name,:required => true
    = f.input :parent_id,:as => :hidden,
              :input_html => {:value => topic_request.id}           
  .actions
    = f.button :submit,"Propose Subtopic",
               :class => "btn",:disable_with => "Please wait..."
#subtopic_requests
  = render :partial => 'topic_requests/subreqs',
           :locals => {:topic_requests => topic_request.subtopics}

部分==> _subreqs.html.slim

- topic_requests.each do |onereq|
  = onereq.name
  = onereq.description
  hr

coffeescript ==> topic_requests.js.coffee

jQuery ->
  new_subreq_form = $("#new_subtopic_request")

  if new_subreq_form.length > 0
    new_subreq_form.bind 'ajax:before',(e) ->
      # console.log 'ajax:before'
    new_subreq_form.bind 'ajax:success',(event,data,status,xhr) ->
      $("#subtopic_requests").html(data)
    new_subreq_form.bind 'ajax:failure',(xhr,error) ->
      # console.log 'ajax:failure'
    new_subreq_form.bind 'ajax:error',error) ->
      # console.log 'ajax:error' # parseerror eg
    new_subreq_form.bind 'ajax:complete',->
      $("#topic_request_name").val("")
      $("#topic_request_description").val("")

问题

子主题创建发生时,我在数据库中看到新的记录.从’ajax:完成’绑定的字段清除也很好,我看到这些输入字段清除.我看到topic_requests / _subreqs.html.slim完成状态200.然而,’show’页面并没有显示渲染部分的结果,这是我试图在’ajax:success’中捕获的结果.

任何有助于调试或抽样的想法,我非常感激.

解决方法

Rails 4解决方案:添加数据{type:’text’}:
= form_for vendor,:remote => true,data: {type: 'text'}

这将data-type =“text”属性添加到窗体中,其中jquery-ujs传递给jQuery的ajax方法作为参数dataType.

根据the docs of jQuery’s ajax method:

>如果没有指定dataType如果是智能推断的
>如果推断出json,则将响应解析为Js对象,并在错误时返回解析错误

这可能是由于您从“ajax:error”获得的parseerror输出发生的情况.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读