Ruby China
  • 社区
  • 招聘
  • Wiki
  • 酷站
  • Gems
  • 注册
  • 登录
Undefined
@losingle
高级会员
第 2743 位会员 / 2012-07-04

长沙
4 篇帖子 / 76 条回帖
3 关注者
3 正在关注
40 收藏
未设置 GitHub 信息。
  • 概况
  • 话题
  • 回帖
  • 收藏
  • 正在关注
  • 关注者
  • Grape 返回 JSON 格式太不灵活 at 2014年09月19日

    #18 楼 @rbprocareer sinatra 也可以用 AR,见 sinatra-activerecord

  • Grape 返回 JSON 格式太不灵活 at 2014年09月02日

    我们现在这样做的,将 rescue_from formate 成约束的协议,然后定义正常的 formatter 和出错时的 formatter

    rescue_from ActiveRecord::RecordNotFound  do |e|
        Rack::Response.new(
            {
                meta:{
                    status: :not_found,
                    msg: e.message
                }
            }.to_json, 404).finish
    end
       ...
       ...
    formatter :json, lambda {|object,env|               
        { :meta => { status: :ok, msg: "" }, :data => object }.to_json
    }
    error_formatter :json, lambda { |message, backtrace, options, env|  
        { :meta => { status: message[:status], msg: message[:msg] }}.to_json        
    }
    

    碰到需要容错的地方,用 grape 提供的 error!

    error!({ msg: 'Email has been registered', status: :registered }, 200)
    
    #返回值
    {
        "meta": {
            "status": "registered", 
            "msg": "Email has been registered"
        }
    }
    
    

    然后正常的返回是这样

    order = Order.first
    present order, with: Entities::Order
    
    # 返回值
    {
        "meta": {
            "status": "ok", 
            "msg": ""
        }, 
        "data": {
            "id": 278, 
            "text": "order1"
        }
    }
    
    #ActiveRecord::RecordNotFound
    {
        "meta": {
            "status": "not_found", 
            "msg": "Couldn't find City with 'id'=22"
        }
    }
    

    Entity(模板)仅仅定义需要对外暴露的 data,而不需要去关心 meta

  • Rails 做服务端,请问移动端的用户验证一般是用什么? at 2014年07月28日

    初期的产品简单的 token 就足以,然后再去慢慢迭代安全相关的特性

    class User < ActiveRecord::Base
    # ..code..
      devise :database_authenticatable,
        :invitable,
        :registerable,
        :recoverable,
        :rememberable,
        :trackable,
        :validatable
    
      attr_accessible :name, :email, :authentication_token
    
      before_save :ensure_authentication_token
    
      def ensure_authentication_token
        self.authentication_token ||= generate_authentication_token
      end
    
      private
    
      def generate_authentication_token
        loop do
          token = Devise.friendly_token
          break token unless User.where(authentication_token: token).first
        end
      end
    
    module API
      class Root < Grape::API
        prefix    'api'
        format    :json
    
        rescue_from :all, :backtrace => true
        error_formatter :json, API::ErrorFormatter
    
        before do
          error!("401 Unauthorized", 401) unless authenticated
        end
    
        helpers do
          def warden
            env['warden']
          end
    
          def authenticated
            return true if warden.authenticated?
            params[:access_token] && @user = User.find_by_authentication_token(params[:access_token])
          end
    
          def current_user
            warden.user || @user
          end
        end
    
        mount API::V1::Root
        mount API::V2::Root
      end
    end
    
  • 如何查询离我 1km 的用户 at 2014年07月28日

    我们用geocoder完全可以达到效果

    distance = 20
    center_point = [40.71, 100.23]
    box = Geocoder::Calculations.bounding_box(center_point, distance)
    Venue.within_bounding_box(box)
    
  • 升级 Mac 时 SSD 和内存的选择 at 2013年05月29日

    3 年多了镁光 M4,一直很稳定

  • 请教一个 Rails Engine 的验证设计方法 at 2013年05月14日

    不太好判断, 初步估计是 dependency 的问题 你的 Pinglun 的 Engine 应该要 dependency 了你的主项目的。

    你应该设计一个基础验证,包含在你的 core 里面,一般不要涉及第三方的认证 Gem,然后再通过 engine 去扩展你的认证,这时候再是用第三方的 Gem 比较合适。 因为你加一个 Rails Engine 的时候,基本都会要用到你的认证

  • mc700 升 ssd,求科普。 at 2013年04月24日

    我的 Pro 15,光驱位一直没换,原来的硬盘位直接换了一个镁光的 M4 256,你就买两个螺丝刀就可以了,一个拧后盖,一个拧硬盘上的固定架(内八角什么的,不太记得了)

  • 萌生了个 `咱社区发展' 的好点子 (大家一起讨论下呗) at 2013年04月15日

    这个就好像要把夫妻生活也搬到线上进行

  • 社区新规则 - 新注册的用户 1 周内禁止发帖 at 2013年03月21日

    这不是解决办法 我觉得可以采用一些机制来过滤。 类似 stackoverflow、HN。

  • 怎么把 erb 换成 slim? at 2013年03月12日

    你可以把 erb 理解成 html http://html2slim.herokuapp.com

    或者装个 gem https://github.com/slim-template/haml2slim

  • 请教多个模型复用 Category 的办法 at 2013年03月12日

    用 polymorphic http://guides.rubyonrails.org/association_basics.html#polymorphic-associations

    不过我也觉得用 tag 比较合适,不过这个 categories 可以作为节点

  • Rails 有什么用户间对话交流功能的开源应用吗? at 2013年03月06日

    #12 楼 @Levan 玩玩这个 Gem https://github.com/LTe/acts-as-messageable

  • Rails 有什么用户间对话交流功能的开源应用吗? at 2013年03月05日

    站内信?

  • Ruby on Rails Tutorial 2nd Edition 译毕 at 2013年03月05日

    @andor_chen 建议把译文放到豆瓣阅读去 😄

  • Ruby on Rails Tutorial 2nd Edition 译毕 at 2013年03月05日

    支持 LZ 工作,PDF 已购买

  • 请教,移动平台即时通讯 (im) 后台架构 at 2013年03月04日

    我们的 IM 产品就是采用的 XMPP 和 Ejabberd,非常不错。 我觉得选用通用的协议更好,像微博 Chat、Gtalk、人人 Chat 都是使用的 XMPP 不过我很不喜欢 XML,这点让 XMPP 的数据传输有些冗余,但是这个不重要,这个不会在你初步阶段成为障碍 移动终端,无论是 iOS 还是 Andriod,还是 Web 包装耗电量和流量消耗都完全是可以接受的。

  • 求 Web 日志分析的工具 at 2013年02月19日

    试试 goaccess

  • RubyConfChina 2012 新浪 SAE Ruby 平台激活码和七牛云存储现金券 at 2012年12月03日

    jinxing.ding#icloud.com

  • RubyConfChina 2012 新浪 SAE Ruby 平台激活码和七牛云存储现金券 at 2012年12月03日

    [email protected]

  • RubyConfChina 2012 新浪 SAE Ruby 平台激活码和七牛云存储现金券 at 2012年12月03日

    losingle#gmail.com

  • 我喜欢的东西去哪里了? at 2012年08月16日

    #1 楼 @zhenning 我不知道,我喜欢了一些什么?也就是说,我喜欢了,还要收藏了,下次我才能快速的找到。

  • 动态地为菜单导航加上 active,大家是怎么处理的? at 2012年08月15日

    一般采用 link_to_unless_current,如果实现不了再自己写 Helper.

  • 上一页
  • 1
  • 2
关于 / RubyConf / Ruby 镜像 / RubyGems 镜像 / 活跃会员 / 组织 / API / 贡献者
由众多爱好者共同维护的 Ruby 中文社区,本站使用 Homeland 构建,并采用 Docker 部署。
服务器由 赞助 CDN 由 赞助
iOS 客户端 / Android 客户端 简体中文 / English