小编tim*_*one的帖子

rails - 保存后如何刷新关联

我有一个包含项目列表的类别.项目有一个位置,类别有一个关系has_many:items,:order =>"position".当用户更新位置值时,我想看到它的位置.我的位置是一个浮点数,允许在舍入数字之间移动.

pos=item.category.items.map(&:id)
current_position=pos.index(id.to_i)
item.save # want to refresh the relationship here
pos_new=item.categoty.items.map(&:id)
# grabbing this since just accessing item isn't updated if positioning has changed
item_new=Item.find(id)
pos_new=item_new.category.items.map(&:id)
new_position=pos_new.index(id)
if current_position!=new_position
  is_moved=true # sent back in JSON to propagate a dynamic change.
end
Run Code Online (Sandbox Code Playgroud)

以上工作,但似乎真的很冗长.有没有办法让我告诉项目保存类别关系需要刷新,因为订单可以更改?

activerecord ruby-on-rails ruby-on-rails-3

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

如何调整图像大小或作为NSAttributedString NSTextAttachment完成(或设置其初始大小)

我有一个NSAttributedString我要添加一个NSTextAttachment.图像是50w乘50h,但我希望它缩小以反映属性字符串的行高.我以为这会自动完成,但我猜不会.我查看了UImage类引用,但是这个图像似乎没有在UIImageView中设置,因此无法访问框架属性.这是我目前拥有的截图:

在此输入图像描述

在理想的世界中,我还想实现一种基于用户输入(例如增加字体大小)来放大图像的方法.关于如何实现这一点的任何想法?

谢谢

编辑1

这是我如何创建它:

    NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
    textAttachment.image = [UIImage imageNamed:@"note-small.png"];
    NSLog(@"here is the scale: %f", textAttachment.image.scale);
    NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];
    [headerAS replaceCharactersInRange:NSMakeRange([headerAS length], 0) withString:@" "];
    [headerAS replaceCharactersInRange:NSMakeRange([headerAS length], 0) withAttributedString:attrStringWithImage];
Run Code Online (Sandbox Code Playgroud)

uiimage nsattributedstring nstextattachment ios ios7

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

转换utf-16 - > utf-8并删除BOM

我们有一个数据输入人员在Windows上以UTF-16编码,并希望拥有utf-8并删除BOM.utf-8转换有效但BOM仍然存在.我该如何删除?这就是我目前拥有的:

batch_3={'src':'/Users/jt/src','dest':'/Users/jt/dest/'}
batches=[batch_3]

for b in batches:
  s_files=os.listdir(b['src'])
  for file_name in s_files:
    ff_name = os.path.join(b['src'], file_name)  
    if (os.path.isfile(ff_name) and ff_name.endswith('.json')):
      print ff_name
      target_file_name=os.path.join(b['dest'], file_name)
      BLOCKSIZE = 1048576
      with codecs.open(ff_name, "r", "utf-16-le") as source_file:
        with codecs.open(target_file_name, "w+", "utf-8") as target_file:
          while True:
            contents = source_file.read(BLOCKSIZE)
            if not contents:
              break
            target_file.write(contents)
Run Code Online (Sandbox Code Playgroud)

如果我hexdump -CI看到:

Wed Jan 11$ hexdump -C svy-m-317.json 
00000000  ef bb bf 7b 0d 0a 20 20  20 20 22 6e 61 6d 65 22  |...{..    "name"|
00000010  3a 22 53 …
Run Code Online (Sandbox Code Playgroud)

python unicode utf-8 utf-16

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

在公共接口和私有接口/实现文件上声明协议之间的区别

以这些方式宣布这些协议之间的区别是什么?只是.h文件中的那些是公开的吗?

in .h file:
@interface TestViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

in .m file:
@interface TestViewController () <UISearchBarDelegate, UISearchDisplayDelegate, UIAlertViewDelegate, MKMapViewDelegate, CLLocationManagerDelegate>
Run Code Online (Sandbox Code Playgroud)

objective-c ios

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

找不到Pods.modulemap - 查找错误的目录

我正在尝试让CocoaPods在Swift项目中工作,但这是我的第一次.

我的Podfile是

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

pod 'Alamofire', '~> 1.2'
pod 'SwiftyJSON', '~> 2.2.0'
Run Code Online (Sandbox Code Playgroud)

我跑pod install但建筑它给了我这个错误:

Error: unable to read module map contents from 'Target Support 
Files/Pods/Pods.modulemap': Error Domain=NSCocoaErrorDomain Code=260
 "The file “Pods.modulemap” couldn’t be opened because there is no such file." 
UserInfo=0x7fac488d5af0 {NSFilePath=/Users/jt/tmp-ios/my-template/Pods/Target 
Support Files/Pods/Pods.modulemap, NSUnderlyingError=0x7fac457f0e90 "The operation 
couldn’t be completed. No such file or directory"}
Run Code Online (Sandbox Code Playgroud)

奇怪的是,这是/ Users/jt/tmp-ios /甚至不是我所在的目录.

Tue Jun 09$ pwd
/Users/jt/repos/EmbersSwift
Tue Jun 09$ 
Run Code Online (Sandbox Code Playgroud)

我之前确实在该目录中有一些东西,它引用的内容尚不清楚.我该如何解决这个问题?我怀疑它是Pods.debug.xconfig中的东西但是希望之前有人处理过这个问题.

ios cocoapods swift

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

将话语留言板与现有的铁路站点集成

我正在寻找为我正在开发的网站集成留言板.新的话语板看起来很有趣http://www.discourse.org/但是没有太多关于通过oauth2或sso或者使用rails引擎类型系统集成到另一个站点的信息.有没有人用这样的应用程序成功设置了这个?

谢谢

编辑
是否存在未来如何进行此整合的路线图?它的一部分看起来非常有趣,但想要了解如何处理这个用例的更多信息.

编辑2
由于这个问题正在得到一个公平的流量,我想添加这篇文章http://meta.discourse.org/t/integrating-discourse-with-current-user-database/6669,它超越了SSO的尝试目前正在进行话语.我也与thougtbot的人们就他们的实施进行了接触.

ruby-on-rails ruby-on-rails-3.2 discourse

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

为使用电子邮件和facebook_id的Web和移动设备创建用户创建/身份验证系统

我正在使用Ruby on Rails为iOS应用程序和Web应用程序(即可以从移动应用程序或Web前端创建用户)编写用户创建系统.我对使用像Devise这样的系统不感兴趣.

我在几年内没有这样做,所以只是想确保我的想法是最新的并且有意义.

步骤/假设

  1. 我们将通过自定义HTTP字段(例如"X-auth_token")管理服务器和客户端之间的状态.
  2. 它们可以通过电子邮件/密码凭证或Facebook创建.

在电子邮件登录的情况下,用户将发布以下json:

{
  user:{
    email:'joe@test.com',
    password:'testpassword',
  }
}
Run Code Online (Sandbox Code Playgroud)

我将使用Rails has_secure_password来散列值.

并将返回

{
  user:{
    id:23,
    auth_token:'md5value',
  }
}
Run Code Online (Sandbox Code Playgroud)

对于web,我们将auth_token作为cookie值传递.对于iOS应用程序,auth_token将作为自定义HTTP标头字段传递,例如"X-auth_token"

  1. 在Facebook登录的情况下,他们将使用FacebookSDK,从FB获得范围的权限,并发布

{ user: { is_facebook_login: true, fb_email: 'joe@test.com', fb_auth_token: 'abigvaluefromFB' } }

在服务器上,我们将通过调用facebook来确保他们传递有效的fb_auth_token凭据

def self.verify_facebook fb_auth_token
  result = Net::HTTP.get(URI.parse("https://graph.facebook.com/me?access_token=#{fb_auth_token}"))
  obj=JSON.parse(result)
  obj["email"]
end 
Run Code Online (Sandbox Code Playgroud)

并验证初始请求中发送的fb_email是否与Facebook提供的电子邮件相对应.

这是带有相应图表的步骤:

脚步

  1. oauth请求到facebook,仅请求发送电子邮件
  2. 用户批准,电子邮件和身份验证令牌返回
  3. 将auth_token发送到我们的服务器; 如果facebook认证客户端,设置标志,以便只能通过Facebook登录
  4. 与facebook验证令牌是否准确

现在不担心,如果他们改变他们的Facebook电子邮件,运气不好.如果他们通过Facebook登录然后想通过电子邮件/密码登录,那就太难了.

在我们的最后,我们将生成一个auth_token(一个MD5字符串),并将其发送回现在管理身份验证的客户端.从那时起,我们将发送自定义HTTP标头X-auth_token

在此输入图像描述

我们会回复

{
  user:{
    id:23,
    auth_token:'md5value',
  }
}
Run Code Online (Sandbox Code Playgroud)

iOS应用程序将通过https://github.com/kishikawakatsumi/KeychainAccess将auth_token写入钥匙串

上述场景对于用户创建是否合理?

authentication facebook ios

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

如何在jQuery中选择data-id和data-action

这是这个问题的后续问题:使用javascript:函数语法与jQuery选择器进行Ajax调用

我该如何选择这个片段?

<div data-id="54" data-action="follow-global-id" class="add-to-list">here is my answer</div>
Run Code Online (Sandbox Code Playgroud)

我有下面的id值并试过这个

 $('.add-to-list[data-action|="action-follow-global-id"][data-id|=id]').text('here is the things jtjt in the id value');
Run Code Online (Sandbox Code Playgroud)

但没有骰子.需要和他们在一起.

请求帮助

jquery jquery-selectors

15
推荐指数
1
解决办法
4万
查看次数

FactoryGirl,Rspec,let和之前的问题

我是第一次使用这些工具.我已经阅读了文档但是想在这里问我到底想要实现的目标.

我有一组用户,我想测试一些我可以在控制器规范中执行的操作.创建每个用户时,会发生一组回调以创建关联对象.

我想访问这些用户实例以及该ActiveRecord类的关联对象.例如,用户将拥有一组列表,因此我希望能够调用user1.lists.

另外,我想在顶部隔离这个设置并使用let或者之前的黑色.似乎只是这样调用let:

# will test that get_count_for_list will return 5
describe ApiController do

  # why same name - seems really confusing!
  let(:user) { FactoryGirl.create(:user) }
  let(:user2) { FactoryGirl.create(:user2) }
Run Code Online (Sandbox Code Playgroud)

不会调用关联的回调.它是否正确?或者它可能是一个时间问题?

我喜欢使用let的语法,并且能够在我的ExampleGroups中访问这些对象,例如user.id,但是无法访问user.lists.目前我正在做的事情如下:

# will test that get_count_for_list will return 5

describe ApiController do

  # why same name - seems really confusing!
  let(:user) { FactoryGirl.create(:user) }
  let(:user2) { FactoryGirl.create(:user2) }
  let(:user3) { FactoryGirl.create(:user3) }

  before do
    FactoryGirl.create(:user2)
    FactoryGirl.create(:user3)
  end
Run Code Online (Sandbox Code Playgroud)

但是觉得必须有更好的方法.我是否创建了这两个用户?

谢谢

编辑1

我在这里分离了有问题的代码.global_id值是通过回调创建的.它在数据库中正确存在,可以通过相应的find_by_email访问,但使用user2 var不提供访问权限.

require 'spec_helper'

# will test that …
Run Code Online (Sandbox Code Playgroud)

rspec ruby-on-rails

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

你可以在Ruby中一行创建/写/附加一个字符串

是否有可能做到这一点?

v='some text'
w='my' + Time.new.strftime("%m-%d-%Y").to_s + '.txt'
File.write(w,v) # will create file if it doesn't exist and recreates everytime 
Run Code Online (Sandbox Code Playgroud)

无需在实例上执行File.open?即只是一个将附加或创建和写入的类方法?理想情况下,红宝石1.9.3溶液.

谢谢

编辑1

这是我根据文档尝试的内容.我没见过rdoc,但看过其他一些例子.我再次询问是否可以通过File.write以附加模式打开文件?谢谢

irb(main):014:0> File.write('some-file.txt','here is some text',"a")
TypeError: can't convert String into Integer
    from (irb):14:in `write'
    from (irb):14
    from /usr/local/rvm/rubies/ruby-1.9.3-p392/bin/irb:13:in `<main>'
irb(main):015:0>


irb(main):015:0> File.write('some-file.txt','here is some text',O_APPEND)
NameError: uninitialized constant O_APPEND
    from (irb):15
    from /usr/local/rvm/rubies/ruby-1.9.3-p392/bin/irb:13:in `<main>'
irb(main):016:0>
Run Code Online (Sandbox Code Playgroud)

ruby

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