Tag: ruby on rails

如何显示未经授权的可以访问的错误

我正在使用Bootstrap,它有div class=”alert notice” ,它有一堆用于各种通知消息的类。 我还有一个用于评论的AJAX销毁操作,我已经添加了cancan授权。 当我尝试删除一条注释时, current_user无权访问它不起作用 – 这是正确的。 但我想要发生的是弹出一个错误信息,在Bootstrap style’d div中持续5-10秒然后消失。 这是我的CommentsController.rb上的destroy动作 def destroy respond_to do |format| if @comment.destroy format.html { redirect_to root_url, notice: ‘Comment was successfully deleted.’ } format.json { head :no_content } format.js { render :layout => false } else format.json { render json: @comment.errors, status: :unprocessable_entity } end end end 我在同一个控制器中的私有方法中设置了@comment […]

Rails最好的方法是在ajax和文档准备好后在Javascript中初始化函数

问题是我调用了一些函数来初始化页面上的一些元素。 但是在ajax成功之后,我必须在多个地方一次又一次地重新调用这些函数。 我想知道旁边使用以下组合 $(document).ready(function(){ function A }); $(document).ajaxComplete(function(){ function A }); 我读到我可以用setTimeout做一些事情并启动线程以延迟来自链接的函数调用http://googlecode.blogspot.com/2009/07/gmail-for-mobile-html5-series-using .html但我很难消化它。 如果有人可以为我分解它。 更新: 我的意思是当我在多个ajax内部更新html时,我必须调用函数A来重新初始化,上面的代码是我的想法,但我认为应该有更好的方法 例 $(document).on(‘click’, ‘a’, function() { $.ajax({ type: ‘GET’, url: ‘some url’, data: data, success: function(data) { $(‘#some-sector’).html(data); function A; <——- to init } }); }); $(document).on('click', 'b', function() { $.ajax({ type: 'GET', url: 'another url', data: data, success: function(data) […]

在rails 3.1中每10秒重新加载一页

我有一个rails 3.1rc4应用程序,我想在每10秒后更新我的index.html.erb文件中的div。 如果可以通过jquery完成,我不会这样做吗? 有一个更新方法,但我不,如果它只是原型。 任何样本都会很棒

Gmaps4rails循环遍历数组

我有一个包含30多个位置的数组,我想在地图上显示。 但是,我没有找到一个演示如何遍历数组以使标记出现在地图上的源,而是选择严格硬编码地图lat / long值。 例如,这是我目前的代码,但它返回错误: allLocations = root.table.rows().data() root.forMap = [] for aLocation in allLocations root.forMap.push(aLocation[9] + ‘, ‘ + aLocation[10]) $(‘#multi_markers’).map -> handler = Gmaps.build(“Google”) handler.buildMap internal: id: “multi_markers” , -> markers = handler.addMarkers(root.forMap) handler.bounds.extendWith markers handler.fitMapToBounds() 注意:我不能简单地使用ruby方法,因为该表还必须与.js.coffee文件中的DataTable数据进行交互。 如何在gmaps4rails方法中循环遍历数组?

如何让Rquery动画在Rails 3.2.8中工作?

我在我的Rails应用程序中使用jQuery的animate()方法。 但是,我无法让它发挥作用。 这是我的application.js文件: //= require jquery //= require jquery_ujs //= require_tree . 我尝试在那里添加//= require jquery-ui ,但它导致我的应用程序崩溃: couldn’t find file ‘jquery-ui’ 谁能告诉我我失踪了什么? 谢谢你的帮助。

如何在Rails上提交模式表单后更新DOM

我正在使用Twitter Bootstrap尝试使用模态表单来处理我的注册过程。 我可以获取提交表单,关闭模式,一切正常,但DOM不会不引人注意地更新。 用户登录后,导航栏(使用部分呈现并具有ruby)不应再具有“注册”链接,并且应获取“注销”链接。 由于我无法在管理表单提交的coffeescript文件中使用render ,如何重新呈现导航栏以便反映成功注册/模式表单提交? 模态forms(HAML): = simple_form_for @user, remote: true, html: {data: {type: ‘html’}, class: ‘form-horizontal’} do |f| %div.modal-header %button.close{“aria-hidden” => “true”, “data-dismiss” => “modal”, :type => “button”} × – if @user.errors.any? #error_explanation %h2= “#{pluralize(@user.errors.count, “error”)} prohibited this user from being saved:” %ul – @user.errors.full_messages.each do |msg| %li= msg %div.modal-body =render ‘form’, f: […]

Rails4:“scrollTop”每隔一次点击都不起作用

scrollTop不会每隔一次点击(每隔一次点击)。 我在页面底部使用will_paginate 。 单击链接时,页面将刷新,显示位置位于页面底部。 然后我点击指向另一个页面的链接,页面刷新,显示位置在我期望的页面顶部。 例如, 单击第2页的链接时,将显示第2页,但显示位置仍保留在底部。 当我点击第5页的链接时,显示第5页,显示位置如我所料位于顶部。 单击第6页的链接时,将显示第6页,但显示位置仍保留在底部。 当我点击第8页的链接时,显示第8页,显示位置如我所料位于顶部。 … 我尝试了以下,我得到了相同的结果。 \资产\ JavaScript的\ calendars.js.coffee $(‘html, body’).animate({ scrollTop: 0 }, ‘slow’) $(‘body’).animate({ scrollTop: 0 }, ‘slow’) $(‘html, body’).scrollTop() $(document).scrollTop() $(window).scrollTop() $(window).scrollTop(0) \意见\日历\ index.html.erb … … 我该如何编辑我的代码?

window.execScript在ie8中失败

我使用jquery 1.9.1在ruby on rails(3.2.11)上工作…在我的应用程序中我开发了一个javascript聊天。 使用IE9一切正常,在IE8中调用时 function refresh_contacts() { url = $(‘#refresh_contacts_url’).val() $.ajax(url) } 我在主题中得到了错误…我试图找出问题并且它似乎是ajax调用(只是调用,即使调用函数上没有代码,所以我认为这是调用本身谁提出问题)… 引发错误的代码(由IE调试器指示)在jquery中(大约603行未缩小,版本1.9.1): globalEval: function( data ) { if ( data && jQuery.trim( data ) ) { // We use execScript on Internet Explorer // We use an anonymous function so that context is window // rather than jQuery in Firefox ( window.execScript […]

jQuery小部件在Rails 4中消失了

我正在做两个项目。 一个有jQuery Datepicker,另一个使用jQuery bxSlider。 我对两者都有同样的问题。 当我导航到这些小部件所在的URL时,小部件不会出现。 只有当我在浏览器上点击刷新时才会出现。 然后,当我导航到另一个链接并再次返回时,小部件再次消失,直到我再次点击刷新。 编辑 我添加了以下内容: $(document).on(‘ready’, function(){ $(‘.slider’).bxSlider({ slideWidth: 230, minSlides: 1, maxSlides: 3, moveSlides: 1, slideMargin: 10 }); });

如何将json响应传递回客户端

在我的ruby on rails代码中,我想将json响应发送回客户端。 由于我是新手上的ruby,我不知道我该怎么做。 我想发送error = 1 and success = 0作为json数据,如果数据没有保存到数据库,如果它成功保存它应该发送success = 1 and error = 0请参阅下面的代码 这是我的控制器 class ContactsController @result.to_json } end else render “new” end end end 这是我的javascript代码 $(‘.signupbutton’).click(function(e) { e.preventDefault(); var data = $(‘#updatesBig’).serialize(); var url = ‘contacts’; console.log(data); $.ajax({ type: ‘POST’, url: url, data: data, dataType: ‘json’, success: function(data) { console.log(data); […]