假设我有对象my_obj
我从数据库中得到这个对象,有了更多的计算,没关系......
然后我需要通过这样的对象迭代,如:
my_obj.each do |m|
some_method(m.id)
end
Run Code Online (Sandbox Code Playgroud)
这段代码不好,因为如果my_obj为nil,我会得到如下错误:
undefined method `each' for nil:NilClass
Run Code Online (Sandbox Code Playgroud)
所以我决定写:
if my_obj.present?
my_obj.each do |m|
some_method(m.id)
end
end
Run Code Online (Sandbox Code Playgroud)
但我认为还有另外一种方法可以做到这一点,如果建筑没有写到处.
那么我怎么能通过对象迭代,只有它不是null?
在我的angularjs应用程序中,我通过json获得了这样的数据示例:
{"id":"1a", "name": "aaa", "emails": {{"123@123.com"}, {"123@123.info"}}},
{"id":"2a", "name": "aba", "emails": {{"23@123.com"}, {"3@123.info"}}},
{"id":"3a", "name": "aab", "emails": {{"3@123.com"}, {"3@123.info"}}},
Run Code Online (Sandbox Code Playgroud)
出于性能原因,我没有使用过滤器进行ng-repeat,但使用ng-show模式......
所以在控制器中我有这样的代码(搜索是我的输入值):
$scope.$watch('search', function(newVal, oldVal) {
$scope.filteredArray = $filter('filter')($scope.users, $scope.search, newVal);
});
Run Code Online (Sandbox Code Playgroud)
但它甚至在id中搜索,例如我输入a并从id获取它,但我不需要字段ID ...
那么如何仅在特定领域中使用过滤器进行搜索?
我如何在两者中使用moment.js:澳大利亚和美国的时间格式?
例如:
07/08/2017 - 对两种时间格式都有好处,但是!
30/08/2017 - 对于moment.js无效,但我可以有这样的dateTime
你可以在这里查看:
我有一个巨大的数据库表,我需要在这里做点什么,但我怎么能做到这一点?如果我写:
@person = Person.all
@person.each do |p|
//something
end
Run Code Online (Sandbox Code Playgroud)
它的负载,负载系统,并需要时间。有什么方法可以通过不将所有数据加载到某个变量来迭代表?
为什么我在邮件上收到默认样式邮件?在代码中我有点风格,但为什么在gmail上我看到默认布局?我的代码是这样的:
!!!
%html
%head
%meta{:content => "text/html; charset=utf-8", "http-equiv" => "Content-Type"}
%title ??????? ?????????????
:css
body, td { background-color:#cecece; color:#000000; font-size:14px; font-family:Verdana,Helvetica,Arial; }
.blue { background-color: blue; height:2px; color: white; }
.notice {margin: 10px auto; background-color: #fbb752; border: 1px solid #cecece; }
%body
.blue
%h3
????? ?????
%b
????????, ??????? ?? ????? ??????????? ????? ?????!
= @order.id
.notice
%table
%tr
%th
id ???????
%th
?????
%th
??????
%th
????? ?????
%th
??????
%th
??????
%tr
%td
= @order.user.id
= @order.user.email …Run Code Online (Sandbox Code Playgroud) 我有这样的架构:
[col-1][col-2]
Run Code Online (Sandbox Code Playgroud)
(所有这些都有12块的宽度)
和我的HTML:
<div class="col-xs-12 col-md-5">1</div>
<div class="col-xs-12 col-md-7">2</div>
Run Code Online (Sandbox Code Playgroud)
是否可以在xs-mobile设备上以这种方式显示?:
2
1
Run Code Online (Sandbox Code Playgroud)
我试过这样:
<div class="col-xs-12 col-md-5 col-xs-push-12">1</div>
<div class="col-xs-12 col-md-7 col-xs-pull-12">2</div>
Run Code Online (Sandbox Code Playgroud)
但它没有帮助(
我有一个简单的HTML表单,我从一个网页获得:
<form id="my">
inputs....
</form>
Run Code Online (Sandbox Code Playgroud)
我需要通过它的ID获取此表单,我知道该怎么做:
@get_doc = Nokogiri::HTML(page)
nb = @get_doc.at_css('#my')
Run Code Online (Sandbox Code Playgroud)
也许我可以通过对象迭代?我需要将所有输入值和输入名称转换为某个变量,然后将其传递给URI.encode_www_form.
我怎样才能做到这一点?我如何获取表单中包含名称和值的所有输入,并将它们传递给encode_www_form?
我有这样的观点(形式偏):
= javascript_include_tag 'car_photos'
= stylesheet_link_tag('jquery.fileupload-ui')
= form_for @car do |f|
- if @car.errors.any?
#error_explanation
%h2= "?????? ? ?????: #{pluralize(@car.errors.count, "error")}"
%ul
- @car.errors.full_messages.each do |msg|
%li= msg
.tab#car-data{style: "height: 1290px;"}
.field
= f.label :manufacturer_id, "?????"
= f.select(:manufacturer_id, options_from_collection_for_select(VehicleManufacturer.all, :id, :name, @car.manufacturer_id), {}, :prompt => "???????? ?????", required: true, id: "manufacturer-select")
.field
= f.label :model_id, "??????"
#models-area
= render :partial => 'models', :object => @models
.actions
= link_to '??????? ? ???????? ??????????', "", id: "go-and-load-photos", remote: true
.tab#car-photo{style: "height: 1290px;"} …Run Code Online (Sandbox Code Playgroud) 我通过服务上传文件:
var addFile = function(files) {
var deferred = $q.defer();
var fd = new FormData();
fd.append("file", files[0]);
$http.post("/files", fd, {
***
})
.success(function(data, status, headers, config) {
***
})
.error(function(err, status) {
***
});
***
};
Run Code Online (Sandbox Code Playgroud)
在控制器中我有类似的东西:
uplService.addFile($scope.files).then(function(url) {
$scope.news.Photo = url;
});
Run Code Online (Sandbox Code Playgroud)
并在HTML视图中:
<input type="file" name="file" onchange="angular.element(this).scope().photoChanged(this.files)" />
Run Code Online (Sandbox Code Playgroud)
在此之前我上传了文件,当我选择文件时它直接进入服务器,但现在我需要在我选择它时以我的形式显示它,但是稍后上传,我在网上看到的只是使用指令,但是如何在不使用指令的情况下组织它?
在我的应用程序中,我尝试连接两个字符串:电子邮件和一些文本.
但我有一个麻烦,我需要文本部分可以"动态"翻译,所以我写道:
$scope.textUnsubscribe = 'SUCESSFULL_UNSUBSCRIBE';
Run Code Online (Sandbox Code Playgroud)
在视图中:
<h4 ng-bind-html="textUnsubscribe | translate"></h4>
Run Code Online (Sandbox Code Playgroud)
我敢肯定(电子邮件+文字):
<h4 ng-bind-html="userEmail"></h4>
<h4 ng-bind-html="textUnsubscribe | translate"></h4>
Run Code Online (Sandbox Code Playgroud)
但后来我有风格错误......
我可以以某种方式放入一个ng-bind-html两个范围变量?一个是"静态的",第二个是可翻译的?
喜欢:
<h4 ng-bind-html="userEmail, (textUnsubscribe | translate)"></h4>
Run Code Online (Sandbox Code Playgroud)