Rails通过ajax渲染部分

时间:2015-02-27 16:13:48

标签: jquery ruby-on-rails ruby ajax render

我似乎无法加载HTML,我获得200状态,服务器显示成功过程,仍然无法执行.js.erb文件内的任何内容。

链接按钮以调用部分: <a> data-target="#user-form-modal" class="register-btn" data-remote="true" data-toggle="modal" data-user-type="1" href="/home/remote_sign_up">Student</a>

路线: get 'home/remote_sign_up', to: 'home#remote_sign_up', as: 'home_remote_sign_up'

主/ remote_sign_up.js.erb

if($('.modal').length > 0) {
    $("#user-form-modal").modal();
}
else {
    $("body").prepend("<%= escape_javascript(render partial: 'remote_sign_up') %>");
    $("#user-form-modal").modal();
}

home / _remote_sign_up.slim 我的模态在这里,我正在打电话。

home_controller.rb

respond_to :html, :json, :js

def index
  @subjects = Subject.all
  respond_to do |format|
    format.html
    format.json { render json: Subject.all.pluck(:name) }
  end
end

def remote_sign_up
 respond_to :js
end

application_controller.rb 我声明了这个:protect_from_forgery with: :exception

回复标题:

Cache-Control:max-age=0, private, must-revalidate
Connection:Keep-Alive
Content-Length:37815
Content-Type:text/javascript; charset=utf-8
Date:Fri, 27 Feb 2015 16:36:48 GMT
Etag:"86956ed8c50442138977628b146e1d80"
Server:WEBrick/1.3.1 (Ruby/2.1.2/2014-05-08)
X-Content-Type-Options:nosniff
X-Frame-Options:SAMEORIGIN
X-Meta-Request-Version:0.3.4
X-Request-Id:11ec0be3-1899-407e-baec-f17b4870ab86
X-Runtime:0.253766
X-Xss-Protection:1; mode=block

预览:

0: 58
1: 58
2: 58
3: "><div class="
4: "><div class="
5: "><button class="
6: " data-target="
7: -1
8: " data-toggle="
9: " type="
10: "><span class="
11: ">Toggle navigation</span><span class="
12: "></span><span class="
13: "></span><span class="
14: "></span></button><a class="
15: " href="

响应:

if($('.modal').length > 0) {
    $("#user-form-modal").modal();
}
else {
    $("body").prepend(Modal code here);
    $("#user-form-modal").modal();
}

我正在接受预览的一部分

2 个答案:

答案 0 :(得分:0)

编辑:在进一步查看代码后,发现了真正的错误。

尝试将format.js添加到您的respond_to块。并将其添加到remote_sign_up方法。

所以它看起来像这样:

def remote_sign_up
  @subjects = Subject.all
  respond_to do |format|
    format.js
  end
end

答案 1 :(得分:0)

很抱歉,我花了很长时间才回答,现在一切正常,我所要做的就是添加thin作为网络服务器并且有效。

相关问题