背景:通常,在我们想要手动指定要在响应中返回的HTTP状态代码的情况下,Rails为我们提供了一组很好的预定义的人类可读Ruby符号 ,而不是明确地使用这些值的数值.码.
我们可以执行以下操作,例如:
render text: "hurray!", status: :ok
Run Code Online (Sandbox Code Playgroud)
这当然最终与此相同:
render text: "hurray!", status: 200
Run Code Online (Sandbox Code Playgroud)
在我的情况下,我想渲染一个自定义的HTTP状态代码(我任意选择了数字242).显然,这段代码不是标准的,并且在Rails中没有符号表示,所以我必须使用实际的数值.
当前解决方案:为了在代码中保持相对人类可读性,我在一个常量文件中放置了一个常量,如下所示:
初始化/ constants.rb
NEEDS_UPDATE_CODE = 242
Run Code Online (Sandbox Code Playgroud)
在我的控制器中,我这样渲染:
render text: "whatever I want to render", status: NEEDS_UPDATE_CODE
Run Code Online (Sandbox Code Playgroud)
问题:这显然工作正常,但我想知道,有没有办法为Rails添加自定义HTTP状态代码的新符号表示?
我想禁用导航控制器的默认后退按钮
self.navigationItem.rightBarButtonItem.enabled = NO;
// Below code does not work since leftBarButtonItem is always nil.
self.navigationItem.leftBarButtonItem.enabled = NO;
Run Code Online (Sandbox Code Playgroud)
我已经手动显示如下,但有没有任何属性禁用默认后退按钮只有一行?
backButton = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, 100, 30)];
[backButton setBackgroundImage:[UIImage imageNamed:@"backbutton_100.png"] forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
[backButton setTitle:@" All Customers" forState:UIControlStateNormal];
backButton.titleLabel.font = [UIFont boldSystemFontOfSize:12];
[buttonView addSubview:backButton];
UIBarButtonItem* leftButton = [[UIBarButtonItem alloc] initWithCustomView:buttonView];
self.navigationItem.leftBarButtonItem = leftButton;
[leftButton release];
// Now it is working.
self.navigationItem.leftBarButtonItem.enabled = NO;
Run Code Online (Sandbox Code Playgroud) 我有以下观点:
window.DmnView = Backbone.View.extend({
template: _.template($("#tmpl_dmnListItem").html()),
events: {
"click .getWhois": "showWhois",
"click .getDomain": "toBasket"
},
initialize: function() {
this.model.bind('change', this.render, this);
this.model.bind('destroy', this.remove, this);
this.bind('toBasket', dmnListApp.toBasket, this);
},
render: function() {
return $(this.el)
.attr("class", this.model.get("free") ? "dmnItem green" : this.model.get("checked") ? "dmnItem red" : "dmnItem red loader")
.html(this.template(this.model.toJSON()));
},
remove: function() {
$(this.el).remove();
},
showWhois: function() {
showBoxes(this.model.get("info"));
return false;
},
toBasket: function() {
this.model.toBasket();
this.trigger('toBasket');
}
});
window.DmnListApp = Backbone.View.extend({
el: $("#regWrap"),
events: {
"keypress #dmnName": "checkAll"
},
initialize: …Run Code Online (Sandbox Code Playgroud) 我在这里阅读了一些关于在应用程序未运行时接收推送通知的问题(超过后台,意味着它完全关闭).特别是这个问题在确定如何使用launchOptions字典确定是否正在接收时非常有用.
但是,我很困惑,我完全承认这可能是我的一个巨大的疏忽:当我的设备在关闭应用程序时收到此应用程序的推送通知,然后我打开我的应用程序时,launchOptions字典是空指针.从前面提到的链接和其他地方接受的答案的描述中,我收集到我应该能够看到通知有效载荷; 但没有什么.我正在为iOS 5.1.1开发.
我唯一的另一个想法是检查启动时的徽章数量(大于零,做某事......),但这似乎非常不可靠.
谁能告诉我我失踪了什么?预先感谢您的帮助!
我最近更新到小牛队.为xcode添加了命令行工具.尝试了"捆绑更新调试器",以及我找到的所有其他修复程序,包括此rake add_source以包含此处找到的相应标头:http://sponsorpay.github.io/blog/2012/06/11/ruby-debugger-and -no-source-for-ruby-error /,我仍然无法将我的项目推送到heroku而没有错误.任何帮助表示赞赏.提前致谢.
更新:找到了这个,但不知道该怎么做."诸如debugger-linecache之类的Gems需要Ruby的显式补丁级别,这对于开发很好,但是不应该在生产中使用.如果你正在生产这些Gems中的一个并且Ruby的补丁升级,你的部署将失败.
解决方法是将您的依赖项移出Gemfile的生产组."这是错误:
安装debugger-linecache(1.2.0)安装debugger-ruby_core_source(1.2.4)安装调试器(1.6.3)Gem :: Installer :: ExtensionBuildError:错误:无法构建gem本机扩展.
/tmp/ruby-1.9.2/bin/ruby extconf.rb
checking for rb_method_entry_t.body in method.h... no
checking for vm_core.h... no
checking for rb_method_entry_t.body in method.h... no
checking for vm_core.h... no
Makefile creation failed
**************************************************************************
No source for ruby-1.9.2-p321 provided with debugger-ruby_core_source gem.
**************************************************************************
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers. Check the mkmf.log file for more
details. You may need configuration options. …Run Code Online (Sandbox Code Playgroud) 我将向您展示两种泛型声明.在part1中,我在List上使用泛型上边界声明如下:
List<? extends Animal> totList = new ArrayList<Animal>();
Run Code Online (Sandbox Code Playgroud)
但是,如果您尝试将Animal对象添加到列表中,则会抛出如下所示的错误:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method add(capture#1-of ? extends Animal) in the type List<capture#1-of ? extends Animal> is not applicable for the arguments (Animal)
at GenericsType.main(GenericsType.java:39)
Run Code Online (Sandbox Code Playgroud)
但在第2部分,如果我声明如下格式的通用类中的名单,没有错误,同时增加(Animal对象)或(Animal对象的子类)到列表中抛出.
class GenericAnimal<T extends Animal>
{
List<T> genList = new ArrayList<T>();
}
Run Code Online (Sandbox Code Playgroud)
为什么在part2中,它没有抛出错误,两种声明风格之间有什么区别.
示例代码:
1.Animal.java
public class Animal {
private String name;
private int height;
public void animalJump()
{
if(height>100)
{
System.out.println(name+" with height-"+height+" can JUMP");
}
else
System.out.println(name+" with height-"+height+" …Run Code Online (Sandbox Code Playgroud) 我正在尝试实现一个解决此问题的程序:
如果给你一个特定的数字(例如:268)
和另外6个数字(例如:2,4,5,25,75,100)
我怎样才能找到能给出确切答案或最接近答案的操作?
您可以使用此操作回答上一个示例:75*4-25-5-2 = 268
规则:
有没有比写出整个可能性并选择最接近的可能性更好的解决方案?因为这个答案会迫使我编写太多行代码,谢谢!
我发现很难理解并使用java中的二进制表示:
在用户Jon Skeet的帮助下,我明白应该以这种方式构建二进制表示.
这是一个代码示例:
public class chack {
public static void main(String[] args) {
int num2=2;
int num3=3;
int num4=4;
int num1=1;
int nirbinary = (num1 << 24) | (num2 << 16) | (num3 << 8) | num4;
System.out.println(nirbinary);
String nir= Integer.toBinaryString(nirbinary);
System.out.println(nir);
}
}
Run Code Online (Sandbox Code Playgroud)
几个问题:
16909060我打印时为什么会这样nirbinary- 它代表什么?如何从已经处于此二进制表示形式的int中获取num1(例如)?谢谢
我正在努力完成非常简单的任务(好吧,我想是这样).我有byte[4]数组,表示颜色值等byte[0] = alpha,
byte [1] = red等.如何将此字节数组转换为实际的颜色对象?谢谢你的回答.
这感觉就像这样一个基本问题,但这对我来说都是新的:
我有一个Person和Room类,它们都有Item对象列表.
public class Person{
private ArrayList<Item> items;
public Person() {
items = new ArrayList<>();
}
public void addItem(){
...
}
public void removeItem(){
...
}
}
public class Room {
private ArrayList<Item> items;
public Room () {
items = new ArrayList<>();
}
public void addItem(){
...
}
public void removeItem(){
...
}
}
Run Code Online (Sandbox Code Playgroud)
项目方法,例如addItem()在Room类和Person类中都是重复的,这不是很好.我想要制作一个单独的库存类,其中包含项目和项目方法的列表,然后每个房间和人员都有一个库存.
但是如果我使用私人库存字段,那么我将无法从人员或房间调用Item方法.
什么是停止复制的最佳方法?提前致谢!