小编DVG*_*DVG的帖子

Rails路线与日期

所以我有一个每周日历视图,我有一条路线设置为接受/:year /:month /:day作为开始日期.

  match "events/(:year/:month/:day)" => "events#index", 
      :constraints => { :year => /\d{4}/, :month => /\d{2}/, :day => /\d{2}/ },
      :as => "events_date"
Run Code Online (Sandbox Code Playgroud)

关于这条路线的使用,我有两个问题.首先,在解析params时,这就是我正在做的事情:

unless params[:year].nil? || params[:month].nil? || params[:day].nil?
  start_date = Date.new(params[:year].to_i, params[:month].to_i, params[:day].to_i)
end
start_date = start_date.nil? ? Date.today : start_date
Run Code Online (Sandbox Code Playgroud)

这让我感到非常啰嗦和丑陋.有没有更好的办法?

当在日历中建立另一周的链接时(对于每周一次的寻呼),我是否必须做类似的事情

#assume an date object with the desired start date
link_to events_date_path(date.strftime('%Y'), date.strftime('%m'), date.strftime('%d'))
Run Code Online (Sandbox Code Playgroud)

这也似乎有点冗长和丑陋.在路线中使用日期的最佳方式是什么?

routes date ruby-on-rails-3

5
推荐指数
1
解决办法
1781
查看次数

清理Paperclip错误消息

好的,所以我有回形针工作,我正在尝试使用内置的验证器来确保上传的文件

  1. 是一个图像
  2. 不是太大了

所以我在模型中有这个,根据文档:

validates_attachment :avatar,
:content_type => { :content_type => /image/ },
:size => { :in => 0..2.megabytes }
Run Code Online (Sandbox Code Playgroud)

但是它在视图中显示的错误是这个混乱:

错误信息

我希望它有点简单,比如"阿凡达必须是一个小于2兆字节的图像"

但是,我无法看到在哪里这样做,因为传递:message => 'something'会抛出错误Unknown validator: 'MessageValidator'

我该如何清理呢?

请注意,上传小图片的快乐路径可以正常工作.

一些进一步的测试表明,上传太大的图像(如桌面背景)或非.rb文件的内容会更优雅地失败,但根本不会显示任何错误消息.仍然不是我想要的.

validation activerecord ruby-on-rails paperclip

3
推荐指数
1
解决办法
2456
查看次数

使用jquery删除行时,nth-child css不会重绘

我有一个jsfiddle,在这里演示了这个问题:http://jsfiddle.net/xjmwY/

基本上,我有一个使用nth-child(奇数)规则的css3斑马条纹表.

.myTable tr {
    background-color: #efefef;
}
.myTable  tr:nth-child(odd) {
    background-color: #fff;
}
Run Code Online (Sandbox Code Playgroud)

当通过jquery删除一行时,我最终得到两个颜色相同的后续行.有没有办法让桌子重新粉刷?

javascript jquery css-selectors css3

3
推荐指数
1
解决办法
2169
查看次数

Jenkinsfile - JsonSlurper 返回字符串而不是地图

所以我正在尝试一个嵌入了 config.json 文件的 CI 构建。

config.json
{
  "some_collection": [
    { "foo": "bar" }
  ]
}
Run Code Online (Sandbox Code Playgroud)

我的詹金斯文件:

import groovy.json.JsonSlurper

node {
  bootstrap()
  test()
}
def bootstrap() {
  stage('bootstrap') {
    git([url: "git@github.com:my-user/my-jenkinsfile-repo.git"])
  }
}

def test() {
  def config = getConfig()
  echo "${config}"
  echo "${config.class}"
}

@NonCPS
def getConfig() {
  new JsonSlurper().parseText(readFile("./config.json")))
}
Run Code Online (Sandbox Code Playgroud)

配置对象的回显显示了文件中的 json,并且 config.class 表示是一个普通的旧字符串。我期待代码返回一个地图。

我尝试过 JsonSlurper 和 JsonSluperClassic,我也尝试过我能想到的所有方法来重组代码以使其更加明确,但我已经没有想法了。

编辑:我尝试添加一些强类型:

def getConfig() {
  JsonSlurper parser = new groovy.json.JsonSlurper()
  def json = readFile("./config.json")
  Map parsedJson = parser.parseText(json)
  return parsedJson
}
Run Code Online (Sandbox Code Playgroud)

这仍然会导致 …

groovy jenkins jenkins-pipeline

3
推荐指数
1
解决办法
2万
查看次数

Dealloc'd Predicate崩溃了iPhone App!

作为序言,这是几天前进行的调查的后续行动:https: //stackoverflow.com/questions/2981803/iphone-app-crashes-when-merging-managed-object-contexts

短版本:EXC_BAD_ACCESS正在崩溃我的应用程序,僵尸模式揭示了我的谓词嵌入在我的Fetched Results Controller中嵌入的获取请求中的罪魁祸首.如果没有明确的命令,对象中的对象如何释放?

长版本:应用程序结构平台视图控制器 - >游戏视图控制器(根据平台选择预测) - >添加游戏视图控制器

当在平台视图上单击一行时,它会在该平台的游戏视图中设置一个实例变量,然后Games Fetched Results Controller以正常方式构建一个获取请求:

- (NSFetchedResultsController *)fetchedResultsController{
if (fetchedResultsController != nil) {
  return fetchedResultsController;
}
//build the fetch request for Games
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription 
  entityForName:@"Game"
  inManagedObjectContext:context];
[request setEntity:entity];
//predicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"platform == %@",
  selectedPlatform];
[request setPredicate:predicate];
//sort based on name
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name"
  ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];

//fetch and …
Run Code Online (Sandbox Code Playgroud)

iphone core-data nspredicate

2
推荐指数
1
解决办法
407
查看次数

删除NSLog会破坏编译器

好的,所以这很奇怪

我有这个代码

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.row) {
  case 1:
    NSLog(@"Platform Cell Selected");
    AddGamePlatformSelectionViewController *platformVC =
      [[AddGamePlatformSelectionViewController alloc]
      initWithNibName:@"AddGamePlatformSelectionViewController" bundle:nil];
    platformVC.context = context;
    platformVC.game = newGame;
    [self.navigationController pushViewController:platformVC animated:YES];
    [platformVC release];
    break;
  default:
    break;
  }
}
Run Code Online (Sandbox Code Playgroud)

哪个工作正常.

当我删除NSLog语句时,如下所示:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.row) {
  case 1:
    //NSLog(@"Platform Cell Selected");
    AddGamePlatformSelectionViewController *platformVC =
      [[AddGamePlatformSelectionViewController alloc]
      initWithNibName:@"AddGamePlatformSelectionViewController" bundle:nil];
    platformVC.context = context;
    platformVC.game = newGame;
    [self.navigationController pushViewController:platformVC animated:YES];
    [platformVC release];
    break;
  default:
    break;
  }
}
Run Code Online (Sandbox Code Playgroud)

我得到以下编译器错误

/Users/DVG/Development/iPhone/Backlog/Classes/AddGameTableViewController.m:102:0 …

iphone nslog uitableview

2
推荐指数
1
解决办法
240
查看次数

自定义UITableViewCell上的UIButton

我有一个自定义UITableViewCell,有几个按钮.当代码全部在一个视图控制器下时,我的按钮声明如下:

myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[myButton addTarget:self
             action:@selector(myButtonAction:)
   forControlEvents:UIControlEventTouchUpInside];
[myButton setTitle:@"Action" forState:UIControlStateNormal];
myButton.frame = CGRectMake(20, 80, 72, 37);
[self addSubview:myButton];
Run Code Online (Sandbox Code Playgroud)

昨晚我将UITableViewCell子类化,所以代码变为:

myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[myButton addTarget:viewController
             action:@selector(myButtonAction:)
   forControlEvents:UIControlEventTouchUpInside];
[myButton setTitle:@"Action" forState:UIControlStateNormal];
myButton.frame = CGRectMake(20, 80, 72, 37);
[self addSubview:damageButton];
Run Code Online (Sandbox Code Playgroud)

但是,由于这样做,按下任何单元格上的按钮会导致操作仅影响表格中的第一行,我不知道为什么.

行动代码:

UIButton *button = (UIButton *)sender;
UIView *contentView = [button superview];
UITableViewCell *cell = (UITableViewCell *)[contentView superview];
NSIndexPath *indexPath = [[self tableView] indexPathForCell:cell];

//do something with objectAtIndex:indexPath.row
Run Code Online (Sandbox Code Playgroud)

我理解将Tag属性设置为indexPath.row以在表视图中使用UIButton是很常见的.但是,我在数据源中使用两个单独的数组来填充TableView的两个不同部分,所以我认为这不会起作用.

iphone uitableview

1
推荐指数
1
解决办法
2592
查看次数