为了简单起见,我有一个更新部分的表单.没什么特别的.在部分内容中,我有一个jQuery可排序列表.在更新表单之前,我可以毫无问题地拖动列表中的内容.
但是,在我更新列表后,就像js没有被重新加载,我必须刷新整个页面才能让事情再次滚动.
我已经尝试了一切,甚至从Ryan Bates Esq借用了演示jQuery应用程序.在这里.在我将部分js shizzle放入部分后,这也无效.
我确定这很简单,但我是一个ajax/jQuery新手,我真的很挣扎.
视图:
#test_one
= render "test_one"
Run Code Online (Sandbox Code Playgroud)
部分"test_one"
= form_for @location, :remote => true do |f|
%table
%tr= collection_select(:location, :type_ids, Type.all, :id, :name, {:prompt => true}, :multiple => true, :class=>"chzn-select")
%tr
%td
%ul#types.unstyled{"data-update-url" => sort_types_locations_url}
- @types.each do |type|
= content_tag_for :li, type do
%span.handle
[Drag]
= type.name
Run Code Online (Sandbox Code Playgroud)
update.js.erb
$("#test_one").html('<%= escape_javascript render("test_two") %>');
Run Code Online (Sandbox Code Playgroud)
在我的控制器中:
def update
@location = Location.find(params[:id])
@types = @location.types.order('location_types.position').limit(2)
respond_to do |format|
if @location.update_attributes!(params[:location])
flash[:notice] = 'Settings …
Run Code Online (Sandbox Code Playgroud) 我一直在与Rails 4和UJS进行斗争,并注意到Rails 4.1的行为(特别是rails-ujs)并不像我期望的那样.
当使用带有确认对话框的远程链接时,UJS驱动程序会在确认弹出窗口被批准之前发送XHR(使确认弹出窗口毫无意义).我本来希望Rails能够处理这些选项的组合,但是我必须编写自己的javascript来执行确认弹出并挂钩到"ajax:beforeSend"事件以防止请求过快触发.
这是我没有工作的东西(请求立即被激活,没有等待确认对话框返回true):
= link_to 'Ajax Button', app_path(resource), method: :delete,
remote: true, id: 'ajax_link', disable_with: 'Wait...',
confirm: 'Are you sure?'
Run Code Online (Sandbox Code Playgroud)
这就是我现在所做的工作:
= link_to 'Ajax Button', app_path(resource), method: :delete,
remote: true, id: 'ajax_link', disable_with: 'Wait...'
$ ->
$("a[data-remote]#ajax_link").on 'ajax:beforeSend', (xhr, settings) ->
return confirm("Are you sure?")
Run Code Online (Sandbox Code Playgroud)
只是我或不应该rails/jquery-ujs处理上述选项组合并等待确认弹出窗口?在过去,Rails 3和之前,远程链接/按钮在AJAX和远程请求时通常"正常工作",但Rails 4似乎使用AJAX需要更多的技巧,这是以前需要的.
还有其他人用Rails 4.1体验过这个吗?还有其他解决方案吗?
我可以使用escape_javascript
js.erb 文件代码在模态内渲染部分:
$("body").append("<%= escape_javascript(render partial: 'example_partial') %>");
$('#my_modal').modal('show');
Run Code Online (Sandbox Code Playgroud)
但是,我似乎无法获得以下结果:
console.log(<%= @error %>)
Run Code Online (Sandbox Code Playgroud) 我<%= simple_form_for @offer, remote: true %>
启用了 Turbolinks。我正在尝试在我的update
操作中使用以下内容重定向并显示成功闪烁:
respond_to do |format|
if @offer.update(offer_params)
flash[:success] = "#{@offer.name} #{t(:updated)}"
format.html { redirect_to seller_offers_path }
format.js
else
format.html { render :edit }
end
end
Run Code Online (Sandbox Code Playgroud)
我的update.js.erb
代码与重定向或闪存无关:
$(".ibox-content.actions").removeClass('sk-loading');
$('.sk-spinner').css("display", "none");
Run Code Online (Sandbox Code Playgroud)
在 application.html.erb 中,我可以用这个渲染 flash:
<div class="row">
<% flash.each do |message_type, message| %>
<%= content_tag(:div, content_tag(:strong, message), id: "flash_message",
class: "text-bold alert alert-#{message_type}") %>
<% end %>
</div>
Run Code Online (Sandbox Code Playgroud)
目前我的表单已保存,但没有重定向。
当我添加Turbolinks.visit(<%= seller_offers_path %>)
的update.js.erb
形式保存,重定向做,但没有闪光灯。
有没有办法让我可以在带有 Turbolinks 的重定向页面上显示 Flash remote: …
我正在使用JQuery 1.4.4和jQuery UJS rails.js(https://github.com/rails/jquery-ujs)制作Ruby On Rails 3.0.3应用程序
我有脚手架生成的模型/视图/控制器 - 文档,我在app/views/documents/index.html.haml文件中删除链接:
link_to "delete", document, :confirm => "Are you sure?", :method => :delete
这会生成以下HTML:
<a href="/documents/1" data-confirm="Are you sure?" data-method="delete" rel="nofollow">Destroy</a>
这似乎很好.
当我点击这个" 销毁 "链接然后我看到浏览器确认(),当我点击取消时,我希望什么都不会发生.
但
删除功能并删除文档.
有没有人有同样的问题,或者我做错了什么?
目前我已经挖掘了这个:
我查看了jquery-ujs rails.js,并且有两个.live()事件附加到同一个锚点:
1)$('a [数据确认],输入[data-confirm]').live('click',function(){...}
2)$('a [数据方法]:不是([data-remote])').live('click',function(e){...}
第一个live()执行返回false; 据我所知,应该制动所有执行链,但事实并非如此.第二个live()仍然执行,第二个live()是生成表单并提交给destroy动作的地方.
我正在提交一个简单的ajax删除请求,如下所示:
#invitation_actions
=link_to "delete", invitation_path(invitation), :method=>:delete, :remote=>true, :class=>"remove", :confirm=>'Are you sure you?'
Run Code Online (Sandbox Code Playgroud)
请注意,由于所有页面中都包含csrf_meta_tag标记,包含请求的页面包含authenticity_token.
当我查看日志时,我看到删除请求缺少authenticity_token:
在2011-04-22 11:21:21 -0700开始DELETE"/ invitations/5"for 127.0.0.1在InvitationsController处理#dump as参数:{"id"=>"5"}
现在,如果我从代码中删除":remote =>:true",即我执行常规页面加载删除工作,我在日志文件中看到以下内容:
在2011-04-22 12:52:19 -0700开始POST"/ invitations/10"for 127.0.0.1在InvitationsController处理#Drown as HTML参数:{"authenticity_token"=>"qlT8uX/WGQeOQSmVZzw1v8rFdSTHDRbzNY0zpSc9mV0 =","id" => "5"}
为什么它是AJAX案例中的DELETE和非AJAX案例中的POST?为什么DELETE不包含authenticity_token?
谢谢你的帮助.
是否有任何捷径:
link_to 'title', path, 'data-type' => :html
Run Code Online (Sandbox Code Playgroud)
我认为:type=>:html
可以工作,就像:remote=>true
转换data-remote='true'
,但不支持.
如果您定义JavaScript函数然后从erb模板调用它,则不会调用该函数.为什么?单击此链接应显示警报,但不会.
= link_to "Add Sprout", new_sprout_path, remote: true
Run Code Online (Sandbox Code Playgroud)
控制器:
def new
@sprout = Sprout.new
respond_to { |format| format.js }
end
Run Code Online (Sandbox Code Playgroud)
然后在模板中我调用一个函数:
# sprouts/new.js.erb
doSomething();
Run Code Online (Sandbox Code Playgroud)
该函数在我的js文件中定义:
# javascripts/sprouts.js.coffee
doSomething = ->
alert "yum ghum I am a new sprout."
Run Code Online (Sandbox Code Playgroud)
JavaScript回来了.我可以在控制台中看到它.但我看不到警报.为什么这不起作用?
我正在使用Rails 4.
我已经阅读/研究了其他答案,但我无法让它发挥作用.我有一个渲染部分的js.erb文件.我想将@node变量传递给它,但我在控制台中收到错误NameError: - undefined local variable or method "node" for #<#<Class:0x007fdcd9163510>:0x007fdcd813b728>
我不确定为什么它不会抓住它.
<div id="tagged_users">
<%= render partial: "nodes/user_tag_list", locals: { node: node } %>
</div>
Run Code Online (Sandbox Code Playgroud)
我想在js.erb中呈现的部分内容
<ul>
<% node.tagged_users.map(&:name).each do |tag| %>
<li><%= tag %>
<%= link_to "x", node_path(node, remove_tag: tag), method: :patch %>
</li>
<% end %>
</ul>
Run Code Online (Sandbox Code Playgroud)
def update
@node = Node.find(params[:id])
if params.fetch(:node, {}).fetch(:autocomplete_tag_names, false)
tag = params[:node][:taggable_email]
@node.user_tag_list.add(tag)
@node.save
respond_to do |format|
format.js { render 'user_tag_add.js.erb' }
end
elseif
# ...
else …
Run Code Online (Sandbox Code Playgroud) 有意见:
# views/users/index.html.erb
<%= link_to 'Destroy', user_destroy_path(user), remote: :true %>
Run Code Online (Sandbox Code Playgroud)
有一个销毁动作:
# controllers/users_controller.rb
respond_to :js
def destroy
@user = user.find(params.require(:id))
@user.destroy
end
Run Code Online (Sandbox Code Playgroud)
呈现:
# views/users/destroy.js.erb
$('#some_div').html('Bye Bye' + user.name);
Run Code Online (Sandbox Code Playgroud)
我想为它编写控制器测试测试
# spec/controllers/users_controller_spec.rb
describe UsersController do
it 'says goodbye' do
delete :destroy, id: user
expect(response.body).to include('Bye Bye #{user.name}')
end
end
Run Code Online (Sandbox Code Playgroud)
但“response.body”始终是空字符串。为什么?如何检查js响应的内容?
ujs ×10
jquery ×4
ruby ×2
ajax ×1
coffeescript ×1
javascript ×1
rspec ×1
simple-form ×1
turbolinks ×1