ExtJS提供了一些很好的帮助函数,如:
在jQuery中有任何等价物吗?我知道我可以将所有三个移植到jQuery,因为我非常喜欢它们,但也许我错过了已经存在的东西.如果可能的话,我想避免与原型混在一起.
最终结果
在我的application_helper.rb文件中,我有一个这样的函数:
def internal_request?
server_name = request.env['SERVER_NAME']
[plus more code...]
end
Run Code Online (Sandbox Code Playgroud)
控制器,模型和视图中需要此功能.所以,我把这段代码放在lib/目录中的实用程序函数文件中.然而,这不起作用:我抱怨request没有被定义.
如何访问目录中request文件中的对象lib/?
在MVC 3项目中使用剃刀助手时出现错误(确实将cshtml文件放在app_code中).看起来生成的代码使用了错误的程序集引用.
使用WebMatrix.Data;
使用WebMatrix.WebData;
编译说:
CS0246:找不到类型或命名空间名称'WebMatrix'(您是否缺少using指令或程序集引用?)
将它们放入GAC并没有改变任何事情.我没有得到它吗?或者这是一个错误?有任何想法吗?
我有一些经常使用的帮助方法,用于将单元测试放入单独的文件中.例如,我的想法是允许我的XYZTests.groovy调用TestHelper.getUserObject()以获得完全初始化的User实例.
现在问题是,springSecurityService.encodePassword(pw)在用户中有一个被调用的东西beforeInsert()总是失败,因为TestHelper.groovy中没有模拟 springSecurityService.
java.lang.NullPointerException: Cannot invoke method encodePassword() on null object
Run Code Online (Sandbox Code Playgroud)
在User.groovy中:
def beforeInsert() {
// ...
password = springSecurityService.encodePassword(pw)
// ...
}
Run Code Online (Sandbox Code Playgroud)
注意:我想避免在 TestHelper.groovy中进行任何模拟,以便在集成测试中使用它的方法.
尽管如此,即使我尝试mockFor()在TestHelper.groovy中调用任何地方,我也会得到一个MME:
No signature of method: static myproject.TestHelper.mockFor() is applicable for argument types: (java.lang.Class, java.lang.Boolean) values: [class grails.plugins.springsecurity.SpringSecurityService, true]
groovy.lang.MissingMethodException: No signature of method: static myproject.TestHelper.mockFor() is applicable for argument types: (java.lang.Class, java.lang.Boolean) values: [class grails.plugins.springsecurity.SpringSecurityService, true]
at myproject.TestHelper.mockSpringSecurityService(TestHelper.groovy:59)
at myproject.TestHelper$mockSpringSecurityService.callStatic(Unknown Source) …Run Code Online (Sandbox Code Playgroud) 我正在将Rails 2.3应用程序升级到Rails 3.在Rails 2.3路由器中,可以:name_prefix在嵌套资源上设置nil以获得更短的名称.实际的URL仍然是完全限定的,但代码可以使用较短的名称.例如,:
map.resources :sites do |site|
site.resources :groups, :as => :groups, :controller => :url_groups, :name_prefix => nil, :member => { :clone => :post } do |group|
group.resources :tests, :as => :tests, :controller => :test_runs, :name_prefix => nil, :collection => { :latest => :get }
end
end
允许一个人使用latest_tests_path.我无法弄清楚如何使用Rails 3做同样的事情,所以我坚持不懈latest_site_group_tests_path.如果这是它需要的方式,我可以通过代码并更改它的每个实例.但我想确保我先没有遗漏任何东西.无论好坏,我确实需要保持URL结构,所以浅路径似乎不是答案.
经过几个小时的搜索,我仍然找不到我的答案L5.
我的问题是:
我想建立一个像这样的链接:
localhost:800/songs/you-drive-me-crazy
Run Code Online (Sandbox Code Playgroud)
但是得到的是:
localhost:800/songs?you-drive-me-crazy
Run Code Online (Sandbox Code Playgroud)
我的路由参数正在变为查询字符串.
//routes.php
$router->bind('songs', function($slug)
{
return App\Song::where('slug', $slug)->first();
});
$router->get('songs', ['as' => 'songs.index', 'uses' => 'SongsController@index'] );
$router->get('songs/{songs}', ['as' => 'songs.show', 'uses' => 'SongsController@show'] );
Run Code Online (Sandbox Code Playgroud)
我在用:
{!! link_to_route('songs.index', $song->title, [$song->slug]) !!}
Run Code Online (Sandbox Code Playgroud)
我已经尝试了一切但尚未成功,你的建议可能会有所帮助.
谢谢.
有人可以建议如何使用函数elasticsearch.helpers.streaming_bulk而不是elasticsearch.helpers.bulk将数据索引到elasticsearch中.
如果我只是改变streaming_bulk而不是批量,则没有任何内容被索引,所以我想它需要以不同的形式使用.
下面的代码从500个elemens的块中的CSV文件创建索引,类型和索引数据到elasticsearch.它工作正常,但我在徘徊是否有可能提高性能.这就是我想尝试使用streaming_bulk函数的原因.
目前我需要10分钟为100MB的CSV文档索引100万行.我使用两台机器,Centos 6.6,8 CPU-s,x86_64,CPU MHz:2499.902,Mem:15.574G总计.不确定它会更快.
es = elasticsearch.Elasticsearch([{'host': 'uxmachine-test', 'port': 9200}])
index_name = 'new_index'
type_name = 'new_type'
mapping = json.loads(open(config["index_mapping"]).read()) #read mapping from json file
es.indices.create(index_name)
es.indices.put_mapping(index=index_name, doc_type=type_name, body=mapping)
with open(file_to_index, 'rb') as csvfile:
reader = csv.reader(csvfile) #read documents for indexing from CSV file, more than million rows
content = {"_index": index_name, "_type": type_name}
batch_chunks = []
iterator = 0
for row in reader:
var = transform_row_for_indexing(row,fields, index_name, type_name,id_name,id_increment)
id_increment = id_increment + 1
#var = transform_row_for_indexing(row,fields, index_name, …Run Code Online (Sandbox Code Playgroud) 如何生成javascript文件的绝对链接.
我假设应该有类似下面的那个(遗憾的是似乎没有):
javascript_url 'main' # -> 'http://localhost:3000/javascripts/main.js'
Run Code Online (Sandbox Code Playgroud)
代替:
javascript_path 'main' # -> '/javascripts/main.js'
Run Code Online (Sandbox Code Playgroud)
我需要绝对URL,因为该javascript文件将用于书签.
另外我需要相同的css文件.
谢谢,
德米特里.
我对Rails相对较新,有点惊讶这不是一个可配置的行为......至少没有一个我能找到的?!?我本来以为的形式99%的人会从空白受益于所有被修剪string和text领域?!?猜猜我错了......
无论如何,我正在寻找一种干燥的方法来从Rails 3应用程序中的表单字段(类型:string&:text)中删除所有空格.
视图有自动引用(包括?)并可用于每个视图的助手......但模型似乎没有这样的东西?!?或者他们呢?
所以目前我做的是首先 需要的,然后 包括 whitespace_helper(又名WhitespaceHelper).但这对我来说似乎并不是很干,但它有效......
ClassName.rb:
require 'whitespace_helper'
class ClassName < ActiveRecord::Base
include WhitespaceHelper
before_validation :strip_blanks
...
protected
def strip_blanks
self.attributeA.strip!
self.attributeB.strip!
...
end
Run Code Online (Sandbox Code Playgroud)
LIB/whitespace_helper.rb:
module WhitespaceHelper
def strip_whitespace
self.attributes.each_pair do |key, value|
self[key] = value.strip if value.respond_to?('strip')
end
end
Run Code Online (Sandbox Code Playgroud)
我想我正在寻找单个(DRY)方法(类?)来放置某个(lib/?),它将获取一个params(或属性)列表,并.strip! 从每个属性中删除空白(?),而不是具体命名.
我正在尝试使用以下代码在我的页面上生成链接
%h2= link_to '{{ label }}', product_path('{{ id }}'.html_safe)
Run Code Online (Sandbox Code Playgroud)
这实际上是一个较大的HTML块的一部分,它将作为一个javascript模板,我稍后将解析使用Underscore.js以填充{{ id }}和{{ label }}占位符.所以我想让rails输出一些东西给我的HTML
/ products/{{id}}
然而,它一直在逃避空间和小礼物,并给予我
<a href="/products/%7B%7B%20id%20%7D%7D">{{ label }}</a>
Run Code Online (Sandbox Code Playgroud)
所以url_helper正在逃避我的字符串,即使我不想要它.我怎么能强迫它不这样做?
我试过了
%h2= link_to '{{ label }}', product_path('{{ id }}'.html_safe)
%h2= link_to '{{ label }}', product_path(raw '{{ id }}')
%h2= link_to '{{ label }}', raw(product_path('{{ id }}'))
Run Code Online (Sandbox Code Playgroud)
和
%h2=raw( link_to '{{ label }}', product_path('{{ id }}'.html_safe))
Run Code Online (Sandbox Code Playgroud)
但它们都不起作用
编辑:
使用它的另一种方法是使用rails console,
include ActionController::UrlWriter
ruby-1.9.2-p0 :010 > product_path '{{ id }}'.html_safe
=> "/products/%7B%7B%20id%20%7D%7D"
Run Code Online (Sandbox Code Playgroud)
任何帮助表示感谢...谢谢
谢谢
helpers ×10
dry ×2
routes ×2
asp.net-mvc ×1
bulk ×1
equivalent ×1
escaping ×1
extjs ×1
grails ×1
groovy ×1
html ×1
jquery ×1
laravel ×1
laravel-5 ×1
php ×1
python ×1
razor ×1
request ×1
ruby ×1
unit-testing ×1
url ×1
validation ×1
whitespace ×1