案件:
我的工作站表格包含一个段塞字段,如果输入一个值,它应该用作段塞.
编辑:一些澄清:
我想要的就像slupress如何在wordpress中工作:
我的问题:
无法弄清楚如何让友情ID使用用户提供的slug.
class Station < ActiveRecord::Base
extend FriendlyId
belongs_to :user
has_many :measures
validates_uniqueness_of :hw_id
validates_presence_of :hw_id
class_attribute :zone_class
self.zone_class ||= Timezone::Zone
friendly_id :name, :use => [:slugged, :history]
before_save :set_timezone!
....
def should_generate_new_friendly_id?
name_changed? or slug_changed?
end
end
Run Code Online (Sandbox Code Playgroud)
编辑:
<%= form_for(@station) do |f| %>
<%=
f.div_field_with_label(:name) do |key|
f.text_field(key)
end
%>
<%=
f.div_field_with_label(:slug) do |key|
f.text_field(key)
end
%>
<%=
f.div_field_with_label(:hw_id, 'Hardware ID') do |key|
f.text_field(key)
end …Run Code Online (Sandbox Code Playgroud) 我正在尝试对所有站点进行查询,并加入Measures
但我只想要最新的措施(由created_at DESC排序),因为一个站有数千个措施.
我试过了
Station.joins(:measures).limit(1)
Run Code Online (Sandbox Code Playgroud)
但这只是限制了电台.
附加信息:
车站有很多措施
措施属于Station
我已经阅读了Active Records文档,并且只有关于在关联中使用where条件的信息.
该应用程序仅针对Postgres,SQL已被接受.
编辑:添加除schema.rb之外:
create_table "measures", force: true do |t|
t.integer "station_id"
t.float "speed"
t.float "direction"
t.float "max_wind_speed"
t.float "min_wind_speed"
t.float "temperature"
t.datetime "created_at"
t.datetime "updated_at"
t.float "speed_calibration"
end
add_index "observations", ["created_at"], name: "index_observations_on_created_at", using: :btree
add_index "observations", ["station_id"], name: "index_observations_on_station_id", using: :btree
create_table "stations", force: true do |t|
t.string "name"
t.string "hw_id"
t.float "latitude"
t.float "longitude"
t.float "balance"
t.boolean "offline"
t.string …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用css列来制作pinterest样式布局.它正在工作,但列中的元素在顶角使用了对角线overflow: hidden.这些色带仅显示在第一列的元素上.我希望它们能够出现在每一栏的所有元素中.如果删除了overflow: hidden所有元素上出现的色带.我做了一个jsfiddle来演示:
https://jsfiddle.net/6ooefvq3/5/
这种行为似乎只发生在Mac上的chrome(49.0.2592.0 canary)上,在safari(9.0.2)上看起来没问题.
知道我可能缺少什么,或需要更改,所以这些色带显示在所有列中的所有元素?
编辑
我正在Leaflet.js地图上绘制SVG标记图标.图标代表气象站,它们根据风向旋转并显示平均风速作为叠加.
我已经能够在Chrome中按照需要运行此功能,但Firefox中的文本位置已关闭.
左边是Chrome(55.0.2883.95),右边是Firefox(50.0.1).
这是Leaflet.Icon我正在使用的自定义类:
window.RemoteWind = window.RemoteWind || {};
// This uses Chroma.js to build a color scale which is used for wind speed.
// http://gka.github.io/chroma.js
RemoteWind.color_scale = chroma.scale([
'lightblue',
'green',
'red',
'purple'
])
.mode('hsl') // the blending mode
.domain([0, 7, 15, 25]); // the distinct steps for each.
RemoteWind.VectorIcon = L.Icon.extend({
options: {
height: 26,
width: 26,
stroke: 'white',
strokeWidth: 2,
circle: {
cx: 13,
cy: 13,
r: 13
}
},
_createSVG: function(){
var svg = document.createElementNS('http://www.w3.org/2000/svg', …Run Code Online (Sandbox Code Playgroud) 我正在尝试实现一个简单的Jasmine测试,其中Jasmine将测试是否在点击输入按钮时运行某些代码.但我无法理解为什么点击不会触发?我意识到如果我只是.click()在beforeEach中有这个功能,但我不认为这是假设工作的方式.
眼镜
describe("export citations", function (){
var btn
beforeEach(function(){
btn= $("input#myButton").eq(0);
});
it("should call click function", function() {
btn.trigger("click");
expect($("#content").length).toBeGreaterThan(0);
});
});
Run Code Online (Sandbox Code Playgroud)
夹具
$(function(){
$("input#myButton").click(function(e){
//Run a bunch of code here
}
});
Run Code Online (Sandbox Code Playgroud) 使用rspec-rails3.0+,测试设置被拆分为spec_helper,rails_helper并且我注意到生成的spec_helper没有require 'rspec/rails'.
这会导致宙斯崩溃:
spec_helper.rb:5:in `<top (required)>': undefined method `configure' for RSpec:Module (NoMethodError)
Run Code Online (Sandbox Code Playgroud)
对这个问题最常见的回应是require 'rspec/rails'.
但是,这不会打破拆分轨道规格和PORO规格的整个目的spec_helper吗?或者这不重要,因为Zeus预先加载了Rails?
我应该在我这样做spec_helper吗?
# Zeus does not preload RSpec
require 'rspec/core' unless defined? RSpec.configure
Run Code Online (Sandbox Code Playgroud)
请注意,在生成的rails_helper包含中:
ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!
Run Code Online (Sandbox Code Playgroud) 我有一个声明为的数组
var arr = new Array();
Run Code Online (Sandbox Code Playgroud)
然后我有一个由服务器返回的对象数组.并且此数组中的每个对象都有三个字段(总是).我必须遍历这个并有条件地添加到arr数组.
由于此arr未预先分配,因此它会在主阵列中达到大量性能.
有没有什么办法可以在我获得主响应数组之后预先分配arr数组,这样我就可以避免这个性能问题?
另外我如何获得对象的大小?
谢谢.
我有这个模型和多对多关联:通过:
class RailwayStation < ActiveRecord::Base
has_many :railway_stations_routes
has_many :routes, through: :railway_stations_routes
end
class Route < ActiveRecord::Base
has_many :railway_stations_routes
has_many :railway_stations, through: :railway_stations_routes
end
class RailwayStationsRoute < ActiveRecord::Base
belongs_to :railway_station
belongs_to :route
end
Run Code Online (Sandbox Code Playgroud)
我添加了列st_index:
add_column :railway_stations_routes, :st_index, :integer
Run Code Online (Sandbox Code Playgroud)
对于路线中的索引站,但我不明白如何从路线视图形式更改它:
ul
- @route.railway_stations.each do |railway_station|
li = railway_station.title
= ????
Run Code Online (Sandbox Code Playgroud) 我为我的网站编写了一个 chrome 扩展,但发现扩展在 chrome 的移动浏览器上不起作用。现在,该网站会检查用户是否安装了扩展程序。如果用户不这样做,网站会要求用户通过内联安装来安装扩展。
问题是,如果用户在移动设备上运行 chrome,我不想提示用户安装扩展程序,因为它不起作用。我想检查浏览器运行扩展的能力,而不是移动设备的用户代理嗅探。Modernizr 似乎没有对此进行检查。有任何想法吗?
Is there a way to programatically create a where clause in Arel where the columns and values are specified separately?
SELECT users.*
WHERE (country, occupation) IN (('dk', 'nurse'), ('ch', 'doctor'), ...
Run Code Online (Sandbox Code Playgroud)
Say the input is a really long list of pairs that we want to match.
I'm am NOT asking how to generate a WHERE AND OR clause which is really simple to do with ActiveRecord.
So far I just have basic string manipulation:
columns = [:country, :occupation]
pairs …Run Code Online (Sandbox Code Playgroud) javascript ×3
activerecord ×2
ruby ×2
arel ×1
bdd ×1
css ×1
css3 ×1
friendly-id ×1
html ×1
html5 ×1
jasmine ×1
jquery ×1
leaflet ×1
modernizr ×1
postgresql ×1
rspec ×1
svg ×1
tdd ×1
zeus ×1