Tag: ruby

成功的Ajax请求成功后,如何在我的视图中增加upvote total

我有一个Thing模型,通过UpVote模型有许多upvotes。 我希望能够通过things/show Ajax创建一个新的UpVote对象,并在不刷新页面的情况下增加upvote总数。 通过Ajax创建一个新的UpVote记录是有效的,但是我不能在视图中增加upvote计数。 如何在成功创建upvote时增加upvote总数? 这是我到目前为止所尝试的: 意见/事/ show.html.erb true, :id => “new_upvote_link”, method: :post, :class => “btn btn-small” %> 意见/事/ create.js.erb $(‘#new_up_vote’).remove(); $(‘#new_up_vote_link’).show(); $(‘#up_votes’).append(‘ @up_vote)%>’); 意见/事/ upvote.js.erb alert(“here”); $(‘#up_votes’).html(”); 控制器/ things_controller.rb class ThingsController < ApplicationController def show @thing = Thing.find(params[:id]) @thing.up_votes.build @up_vote = UpVote.new end def upvote @thing = Thing.find(params[:id]) UpVote.create!(ip: request.remote_ip, voteable_id: params[:id], voteable_type: […]

Twitter Bootstrap Rails按钮下拉列表没有响应AJAX

我有一个使用Bootstrap的Rails 4应用程序。 我正在创建一个仪表板页面,显示颜色编码的按钮,以表示它所代表的工具的状态。 页面中的HTML如下所示: 706-Hydraulic Ram 98061841 In Service Out of Service view Details 链接的两个方法看起来像这样: def in_service @tool.update(:in_service => true) respond_to do |format| format.html { redirect_to(root_path) } format.js end end def out_service @tool.update(:in_service => false) respond_to do |format| format.html { redirect_to(root_path) } format.js end end 由于“format.js”之后没有任何内容,Rails应该自动执行一个与.js.erb后面的方法同名的文件。 所以我有一个in_service.js.erb和一个out_service.js.erb文件。 它们看起来像这样: $(‘.in_service’).bind(‘ajax:success’, function() { $(this).closest(‘.btn’).addClass(‘.btn-success’).removeClass(‘.btn-warning’); }); $(‘.out_service’).bind(‘ajax:success’, function() […]

在rails 3升级后重新定位时出现回形针错误

我有回形针工作上传和保存图像的不同样式但是当我使用来自railscasts教程的jcrop来裁剪图像时它不会裁剪图像。 我收到这个错误 [paperclip] identify -format %wx%h ‘/var/folders/z+/z+KzOZBFE9irCpbMKKBGFk+++TI/-Tmp-/paperclip-reprocess20110118-19757-1wtrjaj-0[0]’ 2>/dev/null [paperclip] convert ‘/var/folders/z+/z+KzOZBFE9irCpbMKKBGFk+++TI/-Tmp-/paperclip-reprocess20110118-19757-1wtrjaj-0[0]’ -crop 28×32+13+15-resize “400×400>” ‘/var/folders/z+/z+KzOZBFE9irCpbMKKBGFk+++TI/-Tmp-/paperclip-reprocess20110118-19757-1wtrjaj-020110118-19757-1a5n558-0.jpg’ 2>/dev/null [paperclip] An error was received while processing: # 这是在rails 2.3.9中工作,但是hvae刚刚升级到rails 3.0.3我已经获得了所有最新的gem等。 cropper.rb文件如下所示,位于初始化程序目录中 module Paperclip class Cropper < Thumbnail def transformation_command if crop_command crop_command + super.join(" ").sub(/ -crop \S+/, '') else super end end def crop_command target = @attachment.instance if […]

Ruby on rails – 更新ajax上的PUT方法

有人能告诉我为什么这个PUT方法不起作用。 $.ajax({ type: “PUT”, dataType: “script”, url: ‘/resources/35’, data: { resource : { pos_y: 45, pos_x: 50 } } }).done(function( msg ) { alert( “Data Saved: ” + msg ); }); 服务器说我已经使用了GET方法但是在我的ajax请求中我输入了:“PUT” Started GET “/resources/35?resource%5Bpos_y%5D=45&resource%5Bpos_x%5D=50&_=1344001820350” for 192.168.1.89 at 2012-08-03 15:50:20 +0200 Processing by ResourcesController#show as */* Parameters: {“resource”=>{“pos_y”=>”45”, “pos_x”=>”50”}, “_”=>”1344001820350”, “id”=>”35”} User Load (0.3ms) SELECT […]

如何使用jQuery AJAX请求和Ruby on Rails’render’方法来实现?

我正在使用Ruby on Rails 3.0.7和jQuery 1.6.1,我想知道如何在AJAX请求之后处理响应xhr , status和ex变量。 AJAX请求是: $jQuery.ajax({ type: “POST”, url: “”, // see the code below error: function(xhr, status, ex) { // handling with ‘xhr’, ‘status’ and ‘ex’ variables } success: function(jqXHR, status, ex) { // handling with ‘xhr’, ‘status’ and ‘ex’ variables } }); 在控制器中我有: respond_to do |format| format.js { render … […]

根据rails中的第一个选择列表值更改第二个选择列表

更新了底部的答案代码 对于第二个选择框,仅显示与所选team关联的staff members选择选项。 形成: 示例案例 :用户选择另一个团队。 工作人员选择选项更新以仅显示与所选团队相关联的工作人员。 我知道解决方案是使用javascript,但我在rails环境中应用它时遇到了麻烦。 我知道这个问题,但仍然无法将其应用到我的rails应用程序。 码 #app/controllers/task_notes_controller.rb … def new @task_note = TaskNote.new @teams = Team.all.includes(:staff_members) end … #app/views/task_notes/_form.html.erb … # Currently displays all staff members on every team, no matter what team is selected. … #app/assets/javascripts/task_notes.js $(document).ready(function(){ $(“#team”).on(‘change’, function(){ alert(“The new team id is: ” + $(this).val() ); # The […]

如何使用Rails 4获得Jquery UI自动完成function?

我正在使用带有jquery-rails(3.1.0)和jquery-ui-rails(4.1.1)gems的Rails 4.0.2。 我正在尝试将自动填充添加到搜索表单中(使用Bootstrap 3.1.0): Search 这是我的应用程序JS和CSS文件: 的application.js //= require jquery //= require jquery_ujs //= require jquery.ui.autocomplete //= require turbolinks //= require_tree . var ready; ready = (function() { $(‘a[href=”‘ + this.location.pathname + ‘”]’).parent().addClass(‘active’); $(“#navbar-search-input”).autocomplete({ source: ‘/products/autocomplete.json’, }); }); $(document).ready(ready); $(document).on(‘page:load’, ready); application.css.scss *= require bootstrap *= font-kit-rails/ubuntu *= require jquery.ui.autocomplete *= require_self *= require_tree . […]

如何从jQuery中调用rails方法

我有这个JQuery代码: $(“p.exclamation, div#notification_box”).live(“mouseover”, function() { }); 我想从jQuery代码中调用这个rails方法作为回调: def render_read self.user_notifications.where(:read => false).each do |n| n.read = true n.save end end 此方法在我的用户模型中。 有没有办法做到这一点?