Tag: ruby on rails

使用ajax请求在同一页面中显示部分内容

我是铁杆新手,我正在尝试为学校做一个简单的项目。 我已经按照以下方式生成了一个脚手架。 rails generate scaffold Book title:string content:text code:string 我正在尝试做的是,当我点击一个链接时,在同一页面上显示一个div的书列表(localhost:3000 / books)。 我试图按照这个程序但它似乎不起作用,当我点击/ books页面末尾的链接时,页面上没有任何可见的渲染 这是我的books / index.html.erb Books Title Content Code my books_controller的索引方法 def index @books = Book.all respond_to do |format| format.html format.js end end _books偏 和books / index.js.erb $(“#content”).html(“”); 当我点击应该呈现部分的链接时,我在终端中得到了这个,并且页面上没有显示任何内容。 Started GET “/books” for 127.0.0.1 at 2017-05-12 17:35:02 +0200 Processing by BooksController#index as JS […]

Rails / facebox_render – 没有定义Ajax(facebox总是加载…)

我试着用facebox_render和rails 3做一个简单的测试。 这是我的步骤: 1.)安装Jquery 1.4.2(Jrails) 2.)安装插件rails插件安装git://github.com/ihower/facebox_render.git 3.)并添加以下行: 在我的application.html.erb中 在我的application_controller中 class ApplicationController < ActionController::Base include FaceboxRender end 当我试图在facebox中加载东西时,我只有加载器,没有任何东西出现…… 例如,在我的home / index.html.erb中 “/images/rails.png” %> 我很高兴知道我错在哪里,在文档中: * facebox_link_to helper, it’s will launch loading facebox first, send ajax request second) * link_to :remote => true, form_for :remote => true …etc Ajax helper (for Rails 3) 所以我觉得我不得不发送ajax请求.. 看了下面的post: 如何使facebox弹出窗口保持打开状态,并且提交后facebox内的内容发生了变化 ,似乎我遇到了同样的问题:我可以在你的firebug控制台中看到“Ajax未定义” […]

Rails:连接到投票的Jquery问题

我刚刚成功创建了一个投票系统,我想让一个按钮同时进行投票,而不是两个按钮。 我正试图绕过js文件,但是我仍然看到两个按钮,有什么建议吗? 谢谢。 微控制器 class MicropostsController < ApplicationController def vote_up @micropost = Micropost.find(params[:id]) current_user.vote_exclusively_for(@micropost) respond_to do |format| format.html { redirect_to :back } format.js end end def unvote @micropost = Micropost.find(params[:id]) current_user.vote_exclusively_against(@micropost) respond_to do |format| format.html { redirect_to :back } format.js end end end JS文件 * vote_up.js * $(“#”).html(”); $(“.”).html(‘<a href="https://stackoverflow.com/microposts//unvote” data-remote=”true” class=”SquekCounterButton b2 “><span class="SquekCounterIcon […]

为什么jQuery代码在我的jasmine测试中没有执行,或者至少没有执行?

我正在使用jasmine-rails gem运行Rails 3.2.8。 以下是我的Gemfile中描述茉莉花设置的行: jasmine (1.2.1) jasmine-core (>= 1.2.0) rack (~> 1.0) rspec (>= 1.3.1) selenium-webdriver (>= 0.1.3) jasmine-core (1.2.0) jasmine-headless-webkit (0.8.4) coffee-script jasmine-core (~> 1.1) multi_json rainbow sprockets (~> 2) jasmine-rails (0.1.0) jasmine jasmine-headless-webkit rails (>= 3.1.0) 我也在使用jasmine-jquery扩展。 这组函数提供了我在测试中使用的toHaveText方法。 我已将jasmine-jquery扩展文件保存到spec / javascripts / helpers目录中。 名为UserInvitationsSpec.js的测试文件直接位于spec / javascripts目录中,其中包含以下内容: describe (“my basic jasmine jquery test”, function(){ […]

需要允许表单重新提交Rails AJAX

我正在编写一个应用程序,我希望能够通过AJAX将人员添加到部门,但是虽然我可以删除多次,但我不能添加多次。 似乎表单无法多次提交。 我正在使用部分(包含两种类型的表单)。 唯一的区别是只有一个“添加forms”和许多“删除forms”(因为你可以同时在许多部门)。 @person.id, :department => department), :method => :post, :class => “remove_form”, :remote => true do %> “btn”%> 是多次生成的 @person.id), :method => :post, :remote => true, :class => “add_form” do %> “btn add-btn”%> 生成一次。 我的javascript是 $(‘.remove_form’).on(‘ajax:success’, function(event,data) { d = jQuery.parseJSON(data) $(‘#departments’).html(d.html) return false }) $(‘.add_form’).live(‘ajax:success’, function(event,data) { d = jQuery.parseJSON(data) $(‘#departments’).html(d.html) return […]

Rails / JS按钮onclick重新渲染部分而不进入DB

我希望用按钮重新加载部分表单(添加)。 我是新手,并且不知道如何简单地在部分列表中显示字段,而单击“添加”按钮多次。 我找不到相关的例子。 当对象保存在DB中时,所有AJAX示例都提到js.erb。 { :f => f } %> ‘degrees/form’, :locals => { :f => f }), class: “btn btn-primary” %> 在这里,@ application是试图显示学位字段的主要forms。 部分只是两个文本字段 – 一个用于选择教育程度,另一个用于选择其细节。这是部分的 { :action => “index” } do |ff| %> ‘span5’ %>

在刷新时显示隐藏的div

我正在努力弄清楚如何使我的代码正常运行。 我目前有一个div在加载时隐藏,一旦从dropbox中做出选择就会更改。 从保管箱中选择(美国)后,将显示隐藏的div,并且用户将继续填写表单。 当用户保存表单时,我的问题就出现了,然后回到那个页面 – > div不再可见(我相信这是因为它的固有显示值设置为“none;”)。 用户必须再次从下拉列表(美国)中选择适当的值才能查看div。 我想知道如果在Dropbox中正确的值,我可以在页面刷新时使div可见。 我在HAML编程,所有选项值(美国,国际)都位于constants.rb文件中。 我相信这可以通过cookie(我希望尽可能避免)或者通过页面加载时的某种jscript / jqueryvalidation来实现…不幸的是我对这些事情都不熟悉,所以我请求任何帮助。 我真诚地感谢您抽出宝贵时间帮助我。 谢谢! HAML代码: = f.select :location, LOCATIONS, {}, :onChange => “showStates(this)”, :prompt => true #states_div{:style => “display:none;”} %div{:class => “label_leftalign field”} = f.label :state, “State” = f.select :state, STATES, :prompt => true 使用Javascript: function showStates(obj) { if(obj[obj.selectedIndex].value == ‘United States’) { document.getElementById(‘states_div’).style.display […]

JQuery函数阻止Rails远程调用

我的页面上有一个链接, passcheck_path进行AJAX远程调用,并且它周围的p标签提供了激活覆盖整个页面的JQuery模式框的function。 true %> JS如下: $().ready( function() { $(‘.blackoutwindow’).jqm({ modal: true, trigger: ‘.blackout’, overlay: 100 }); }); 当我单击链接时,会出现模态,但远程调用根本不会发生。 但是,当我删除p标签时,远程调用工作正常(但显然模式不会触发)。 我真的不知道为什么它会像这样,但我认为javascript覆盖了什么? 非常感谢您的帮助。

只有最后一篇文章的评论显示在弹出窗口(Ruby on Rails)

我在每个post下都有评论。 所有post和评论都可以通过链接/用户/用户名查看 我正在使用facebox(基于jQuery的灯箱)弹出窗口,评论显示得很好。 唯一的问题是弹出窗口中只显示最新post的评论。 所有弹出链接都显示相同的注释。 例如,如果最新post有5条评论,我可以点击post下面的评论链接,在弹出窗口中查看。 但是,当我向下滚动并转到带有1条评论的其他post时,当我点击评论链接时,我会看到相同的5条评论。 如何确保仅显示与特定post关联的评论? 这是我的show.html.erb jQuery(document).ready(function($) { $(‘a[rel*=facebox]’).facebox() }) Browse 我的micropost.html.erb,评论视图位于底部 ‘Avatar’, :class => ‘avatar’, :height => “10%”, :width => “10%”) %> ago 最后是comment_form.html.erb Comment “facebox-#{micropost.id}” do %> Comments

茉莉花 – 触发事件不起作用

我正在尝试测试的代码: $(“.toggle_item”).on(“change”, function() { console.log(“change triggered”) item_name = $(this).data(“name”); value = $(this).prop(“checked”); if (Item.isValid(item_name) && cartModule.isUnique(item_name)) { cartModule.toggleItem(item_name, value); } }) 茉莉花规格: describe(“changing toggle item”, function() { beforeEach(function() { console.log(“in spec”) affix(“.toggle_item[data-name=’ladder’]”) spyOn(cartModule, “isUnique”) spyOn(Item, “isValid”) $(“.toggle_item”).trigger(“change”) }) it(“should check item for validity & cart for uniqueness”, function() { expect(Item.isValid).toHaveBeenCalledWith(“ladder”) expect(cartModule.isUnique).toHaveBeenCalledWith(“ladder”) }) }) 控制台日志输出表明触发器未触发。 日志输出: […]