Tôi mới tham gia RoR, nhưng có thành công tốt với việc triển khai các tính năng khác nhau cho một ứng dụng nhỏ. Cho đến khi tôi nhấn vấn đề này .. mà chưa tìm thấy bất kỳ vấn đề/câu hỏi hiện có nào. Để giúp thu hẹp vấn đề của tôi đến một hình thức chung chung, đây là những gì tôi có:ajax: hỏa hoạn hoàn toàn nhưng ajax: thành công không nằm trong đường ray 3 ứng dụng
tài Câu chuyện
tài được trình bày với một hình thức để tạo ra chủ đề (tên & mô tả), một khi chủ đề được tạo ra sử dụng là trình bày trang 'hiển thị' cho phép người dùng thêm chủ đề phụ. Khi người dùng thêm chủ đề phụ, họ được hiển thị cho người dùng trên cùng một trang (đây là nơi tôi đang cố gắng sử dụng ajax).
Artifacts Mã
mô hình ==> topic_request.rb
class TopicRequest < ActiveRecord::Base
has_many :subtopics, :class_name=>"TopicRequest", :foreign_key=>"parent_id"
end
điều khiển ==> 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 :description, :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}
phần ==> _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, status, error) ->
# console.log 'ajax:failure'
new_subreq_form.bind 'ajax:error', (xhr, status, error) ->
# console.log 'ajax:error' # parseerror eg
new_subreq_form.bind 'ajax:complete', ->
$("#topic_request_name").val("")
$("#topic_request_description").val("")
Vấn đề
Tạo chủ đề phụ xảy ra, tôi thấy các bản ghi mới trong cơ sở dữ liệu. Thanh toán bù trừ của các lĩnh vực từ 'ajax: hoàn thành' ràng buộc cũng xảy ra tốt, tôi thấy những lĩnh vực đầu vào thanh toán bù trừ lên. Tôi thấy topic_requests/_subreqs.html.slim hoàn thành với trạng thái 200. Tuy nhiên, trang 'show' không hiển thị kết quả hiển thị phần, đó là những gì tôi đang cố chụp trong 'ajax: success'.
Bất kỳ ý tưởng nào để giúp gỡ lỗi hoặc các mẫu mà tôi tham chiếu để thực hiện việc này được đánh giá cao.
Điều này phù hợp với tôi. –
Hoạt động! cảm ơn rất nhiều. – Ritikesh