因此,我有一个非常简单的模型,其中设置了嵌套的属性:class Property < ActiveRecord::Basehas_many :treesaccepts_nested_attributes for :trees end然后我在控制器中构建它们:class PropertiesController < ApplicationControllerdef new@property = Properties.new@trees = @property.trees.buildend end然后在我看来(使用苗条):= form_for @property |p|= p.text_field :name.tree= p.fields_for :trees do |t|= t.text_fiel...
在我们的应用程序中导航时如何禁用浏览器后退按钮.我已经尝试使用以下代码来防止仅返回选项,而不能隐藏或禁用返回导航图标.<script type = "text/javascript" >history.pushState(null,null);window.addEventListener('popstate', function() {history.pushState(null,null); }); </script>和<script type="text/javascript">window.history.forward();</script>我想知道至少能够屏蔽该浏览器的后退导航图标/按钮(就像在浏览器中打...
Rails 3,JRuby 我最近参加了jQuery中的速成课程,其中包括一些Ajax部分渲染.这让我开始思考,我是否可以使用它通过setInterval()轮询Rails服务器,每隔x秒不断刷新页面的特定部分? 我遇到的问题是如何使用$.get()方法获取部分的网址并使用load()重新加载.这就是混乱的开始-使用Rails 3,我有一个部分称为“ _microposts”,它在div中以’id =“ gf”(gf表示全局供稿)呈现.这发生在我的Rails应用程序主页上,因此在这种情况下,URL为“ //...
我使用Working with Javascript in Rails指南中的remote:true成语:# new.html.slim = form_for @thing, remote: true do |f|f.text_field :whateverf.submit 'Submit' # thing_controller.rb layout 'foo'def create end # create.js.erb alert('foobar')这失败了,因为出于某种原因create.js.erb呈现在’foo’布局中,并以html而不是javascript的形式返回,尽管事实上该请求已被正确地处理为Javascript:Processing by ThingsContr...
Iam在Rails中构建表单.我想通过ajax提交,因此我将remote设置为true<%= form_for @post, html: { multipart: true }, remote: true do |f| %>... <%= end %>在我的控制器中:def create@post = Post.new(post_param)respond_to do |format|if @post.saveformat.html { redirect_to @post, notice: 'Post was successfully created.' }format.json { render :show, status: :created, location: @post }elseformat.html { render js...
我在将宝石加载到Rails 4应用程序时遇到问题. 我使用jQuery.在我的application.js中,我有://= require jquery //= require bootstrap-sprockets //= require jquery_ujs //= require html.sortable //= require disqus_rails //= require moment //= require bootstrap-datetimepicker //= require pickers //= require main //= require hammer.min //= require jquery.animate-enhanced.min //= require jquery.countTo //= req...
我需要将ruby数组放入javascript数组,并且遇到解析错误.var characters = <%= @poop.to_json %>;这就是我将ruby嵌入到嵌入式javascript中的方式,并提出了解析错误.我应该如何将ruby数组放入javascript中? 我将其嵌入到.html.erb文件中,因此ruby应该在javascript之前到达变量. 野生动物园控制台正在显示以下内容: 更新 我有一个表单,该表单分别具有三个字段js,css和html,所以我将javascript放入表单中,然后将其放入标头中.<head><...
我需要在我的网站上有一个按钮,可以将信息发送到控制器的创建操作(“页面时间”).它似乎正在工作,虽然它没有发送我指定的所有数据 – 可能与我无法构建数据向量有关.我通过post’pagetimes / create’在我的config / routes.rb文件中提供了POST请求 在application.js中:function submitForm() {alert("checked the button - worked");$.ajax({type:'POST', url: '/pagetimes/create', data: { pagename: "whatever", start: 7, en...
在我的index.html.erb里面<script type= "text/javascript">var msg = "Hello World" ; </script>我需要将此var msg传递给我的控制器,例如我的Post控制器中的get_variable()方法. 编辑:在同一个index.html.erb中获取javascript变量msg作为ruby变量提前致谢解决方法:尝试这个<%= link_to_remote, 'Message', :url=>{:controller=>'controller_name', :action=>'method_name'}, :with=> "'msg='+msg" %>controller_name.rbdef metho...
我有一个Rails 4表单,它在表单页面上使用AJAX构建部件列表.一旦在< ul>中构建了部件列表.我想在params散列中的参数中将列表作为值数组提交. 我的表格:<%= form_for ([@tool, @service]),:html => { :onSubmit => 'getParts' } do |f| %><% if @service.errors.any? %><div id="error_explanation"><h2><%= pluralize(@service.errors.count, "error") %> prohibited this service from being saved:</h2><ul><% @service.errors.f...
我使用validates_format_of方法来检查电子邮件格式:validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i我也使用livevalidation插件来验证表单,所以在我的代码我得到:(?i-mx:^([^@\\s]+)@((?:[-a-z0-9]+\\.)+[a-z]{2,})$)Javascript无法读取此正则表达式.如何或在何处我可以将此正则表达式更改为原始:/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i?解决方法:Ruby和JavaScript正则表达式由具有不...
我正在使用coffeescript ruby??-on-rails.如果咖啡代码中存在语法问题,将coffeescript编译为纯JS会导致错误.在我运行应用程序时的浏览器上,我收到此错误:ExecJS::RuntimeError 有什么办法可以找出导致编译错误的原因,当它试图将咖啡代码编译成JS时?附件是error的屏幕截图 以下是服务器日志中的条目,它无法指示实际导致语法错误的文件:ActionView::Template::Error (SyntaxError: [stdin]:25:10: unexpected :):4: <meta nam...
现在我通过使用browserify-rails来编译js文件,在Ruby on Rails应用程序(使用react-rails gem)上制作React应用程序. 所以我试图让我们react-router配置应用程序的路由器 这是我的main.jsimport React from 'react'; import ReactDOM from 'react-dom'; import { Router, Route } from 'react-router'; import { browserHistory } from 'react-router';/*Import Component*/import DashBoard from './components/dashboard'; import...
我正努力让我的谷歌地图在我的Rails应用程序中运行. 一切正常 – 我继续研究下一个功能,并回来发现它不再适用. 我有地址,个人资料和项目的模型.我在每个配置文件和项目中使用地址在每种情况下,地图都不会显示. 模型之间的关联是: 地址belongs_to :addressable, :polymorphic => true轮廓has_many :addresses, as: :addressableaccepts_nested_attributes_for :addresses, reject_if: :all_blank, allow_destroy: true项目has_man...
我正在Ruby on Rails中创建一个网站.我有一个控制器动作,呈现如下视图:def showtime_left = Time.now.to_i - 3.hours.to_i@character = current_user.characters.find(params[:id])respond_to do |format|format.html # show.html.erbformat.xml { render :xml => @character }end end这很好,因为它像我喜欢的那样呈现show.html.erb.然而,我想以某种方式将time_left作为Javascript变量传递给视图,因为这个值被倒计时JQuery插件使...