Tag: 的Ruby on 轨道

Rails使用URL哈希路由(window.location.hash)

有没有办法在routes.rb或控制器中获取URL的哈希值(例如/ posts / show#35中的“35”)? 我的印象是这从未发送到服务器,但我只是想确定。 谢谢! 汤姆

将ActiveRecord对象的所有属性(除了id,created_at,updated_at除外)设置为nil的最简单方法是什么?

将ActiveRecord对象的所有属性(除了id,created_at,updated_at除外)设置为nil的最简单方法是什么?

Rails:渲染集合部分:获取部分内部集合的大小

我有一个我想用部分渲染的项目集合: @items = [‘a’,’b’,’c’] @items, :partial => ‘item’ %> 我想用升序数字对元素进行编号 。 所以输出应该是: 3: a 2: b 1: c 我知道rails在partial中提供了一个计数器,所以如果我想对降序的项目进行编号,我可以创建以下部分: : 这给了我 1: a 2: b 3: c 但是对于递增的数字,我需要项目的总数,我可以提供一个局部的部分: @items, :partial => ‘item’, :locals => {:total => @items.size} %> 然后在部分: : 但是我感觉像重复一样,因为渲染方法已经知道了集合的大小。 除了使用局部变量之外,真的没有办法让集合中的项目总数达到局部吗?

bootstrap:要导入的文件未找到或不可读

我已经找到了一些关于我的问题的主题,但我不明白应该如何修复它。 我正在关注http://ruby.railstutorial.org/chapters/filling-in-the-layout#fnref-5_5教程,并在app / assets / stylesheets / custom.css.scss中导入引导程序。 那是我的Gemfile: source ‘https://rubygems.org’ gem ‘bootstrap-sass’, ‘2.0.4’ gem ‘rails’, ‘3.2.8’ group :development, :test do gem ‘sqlite3’, ‘1.3.5’ gem ‘rspec-rails’, ‘2.11.0’ end Gems used only for assets and not required in production environments by default. group :assets do gem ‘sass-rails’, ‘3.2.5’ gem ‘coffee-rails’, ‘3.2.2’ gem ‘uglifier’, ‘1.2.3’ end gem […]

停止Rails表单标签修改文本

我在其中一个视图中有以下代码,但它总是呈现为主页url。 如何阻止它改变案例?

如何在Rails 4中允许带散列的数组?

Rails 4中有以下参数: {“delivery_time”:”10″,”delivery_type_id”:”1″,”order_items”:[{“count”:”5″,”item_id”:”1″}],”order_status_id”:”1″,”user_id”:”1″,”action”:”create”,”controller”:”api/v1/orders”} 有以下强烈的参数: params.permit(:user_id, :order_status_id, :delivery_type_id, :delivery_time, order_items:[]) 但是这段代码返回没有’order_items’数组的哈希。 我认为原因是数组中的哈希。 请告诉我,我该如何解决? 提前致谢

通过Elasticsearch / Tire的时间表查找开放式商店

我有模型Shop每个都与Timetable有关系,可能包含以下内容: shop_id: 1, day: 5, open_hour: 7, open_minutes: 0, close_hour: 13, close_minute: 30 shop_id: 1, day: 5, open_hour: 14, open_minutes: 30, close_hour: 18, close_minute: 00 当然, Timetable可以有更优雅的格式,但问题是下一个:如何使用elasticsearch(轮胎)我可以找到开放的商店吗? 所有的想法都会被贬低! 谢谢! 找到解决方案 为每一天创建单独的索引(星期日,星期一,…) 每天从Timetable构建完整的分钟数: ((open_hour * 60 + open_minute)..(close_hour * 60 + close_minute)).to_a 添加filter进行搜索: filter :term, current_day_name => (current_hour * 60 + current_minutes) 这个解决方案同样有效,但看起来很麻烦,因为如果Shop每天工作8小时,我创建了大小为8 * 60 = […]

eager_load = true的影响是什么?

我需要知道为什么在非生产环境中首选eager_load是false ? 我听说过的一个论点是eager_load eager将大部分Rails和应用程序加载到内存中。 因此,使用eager_load进行单独测试会使其运行速度变慢。 然而,这引发了一些问题,比如如何在不加载Rails和应用程序相关代码的情况下运行测试? 什么是正在加载的Rails和应用程序相关代码? config.eager_load_namespaces提供以下类: ActiveSupport ActionDispatch ActiveModel ActionView ActionController ActiveRecord ActionMailer Jquery::Rails::Engine MyApp::Application 是否所有这些类及其子类都是急切加载的? 在开发或测试环境中使用eager_load = false有哪些明显的缺点?

回调后的`attribute_changed?`的行为将会改变

我刚刚将一个项目升级到Rails 5.1.0,我看到了这个弃用警告。 DEPRECATION WARNING: The behavior of `attribute_changed?` inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after `save` returned (eg the opposite of what it returns now). To maintain the current behavior, use `saved_change_to_attribute?` instead. 我的代码看起来像这样 class MyClass […]

在routes.rb中访问URL帮助程序

我想使用以下行重定向路径中的路径: get ‘privacy_policy’, :controller => :pages, :as => ‘privacy_policy’ get ‘privacypolicy.php’ => redirect(privacy_policy_url) 这样/privacypolicy.php就会被重定向到正上方定义的正确页面。 但是,它抛出以下错误: undefined local variable or method `privacy_policy_url’ 所以我猜测不能在routes.rb中使用URL助手。 有没有办法在路由文件中使用URL帮助程序,是否可以这样做?