Ruby China
  • Topics
  • 招聘
  • Wiki
  • 酷站
  • Gems
  • Sign Up
  • Sign In
lychee
@lolychee
VIP
NO. 452 / 2011-12-12

北京
1 Topics / 96 Replies
2 Followers
0 Following
2 Favorites
GitHub Public Repos
  • authcat 6

  • manyou 1

    A lightweight Community Forum base on Rails3 and Mongodb

  • mongoid-association_scope 1

  • vscode-docs 0

    Public documentation for Visual Studio Code

  • dotfiles 0

  • boost 0

  • actions-build-padavan-... 0

    GitHub Action 学习实例 - 自动编译 padavan 和 openWrt

  • grape-entity 0

    An API focused facade that sits on top of an object model.

  • lolychee.github.io 0

    personal blog

  • terraform-provider-qin... 0

    Terraform QingCloud Provider

More on GitHub
  • Overview
  • Topics
  • Replies
  • Favorites
  • Following
  • Followers
  • 如何优雅的重构这段代码? at July 17, 2015
    def nested_transaction(*classes, &block)
      classes.reverse_each.inject(block) {|nested_block, klass| -> { klass.transaction(&nested_block) } }.call
    end
    

    这个和 #1 楼 @kayakjiang 的逻辑是一样的。

  • ActiveRecord 有无按数量切割结果的内置方法? at July 11, 2015
    (1..10).each_slice(3).to_a 
    # => [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]
    
  • 子类中怎么跳过父类的 validate 方法 at June 28, 2015
    
    class Parent < ActiveRecord::Base
    
      with_options if: :is_a_parent? do
        validates :name, presence: true
        validates :age, presence: true
      end
    
      def is_a_parent?
        is_a?(Parent)
      end
    
    end
    
    class Child < Parent
      # do nothing
    end
    
    

    我没跑过,感觉应该可以

    如果是跳过全部验证,用record.save(validate: false)更简单

    以及我也觉得你是不是某些地方没搞对,如果是某些验证在子类的行为不同,你可以把验证写成方法,然后在子类覆盖啊

  • 表中没有 Primary Key 怎么办?( 暂时解决 ) at June 23, 2015

    试一下 self.primary_key = nil

  • 关于 UserMailer.welcome (@user).deliver_later 使用中遇到的一个问题 at June 20, 2015

    你遇到的问题是异步任务里收到的参数和当初传进来的不一样,为什么不一样,是因为 ActiveJob 序列化参数的时候,某些对象使用 class/id对 这样的方式存储。 http://guides.rubyonrails.org/active_job_basics.html#globalid

    如何解决:

    1. 持久化,即 Rei 所说
    2. 通过参数传过去,示例:UserMailer.account_activation(self, activation_token: 'token').deliver_later
  • 最近每晚在斗鱼 TV 直播学习 Ruby、RoR,开个帖记录下,欢迎围观! at June 20, 2015

    你这预告发的也太晚了吧,今天都 20 号了,今天晚上也直播吗?

  • Active Record 中如何使用 delegate 到一个不相关的 class 里的方法 at June 15, 2015
    class BaseHandler
    
      module ModelMethods
        def pay
        end
      end
    
    end
    
    class Order < ActiveRecord::Base
      include BaseHandler::ModelMethods
    end
    

    写个模块然后引入到 Order 里

  • 关于 carrierwave 如何获取 original_filename 值感想 at May 27, 2015

    你用public :original_filename就可以把他变成公开的了。

  • 求教一下用 API (HTTP GET Request, Product Hunt)【已解决】 at May 20, 2015

    #2 楼 @cqcn1991 你改成 https 试试

  • 求教一下用 API (HTTP GET Request, Product Hunt)【已解决】 at May 20, 2015

    我猜你 access_token 前面没加"Bearer ",我记得标准里这个部分好像是必须加的,用来指示验证方法,其他的值还有 Basic、MD5 等等……

  • Rails 能不能导出一份 migration 文件包含以前所有的内容? at May 19, 2015

    http://guides.rubyonrails.org/active_record_migrations.html#schema-dumping-and-you

  • 敬告各位发招聘的公司注意! at May 14, 2015

    招聘版添加审核好了,不合要求的打回重写。 不在招聘版发招聘贴直接封号。 比现在发出来再删要好。

  • 网上查了好多资料都没解决,关于 Ruby 如何在 action 中返回 XML 问题 at April 27, 2015

    在你的程序里设置 HTTP Header Accept: application/xml,或者在你的 url 后面加上.xml。推荐使用前者。

  • yml 文件中嵌入 erb,i18n 如何重新加载 yml? at April 27, 2015

    http://guides.rubyonrails.org/i18n.html#passing-variables-to-translations translation 可以传参数的

  • 那些不常更改的内容,你们怎么设置的? at April 27, 2015

    = = 你建一个 pages 的表存起来就行了,然后加上缓存。存成文件什么的更复杂。

  • 总结一下 Ruby 中的对象和类模型 at April 26, 2015

    非常好的文章,我再补充点: 文章中说的 eigenclass 其实有方法找出来,就是Object#singleton_class,自 1.9.2 版本时添加的方法。

    Object.new.singleton_class  #=> #<Class:#<Object:0xb7ce1e24>>
    String.singleton_class      #=> #<Class:String>
    nil.singleton_class         #=> NilClass
    

    然后,关系图这里也有一个 http://ruby-doc.org/core-2.2.2/Class.html

  • 实现项目各阶段工作的理解 at April 26, 2015

    说实话,没看懂。

  • 一个测试 JSON API 时用来对比 JSON 内容的 helper,以及请教是否有更优雅的写法 at April 22, 2015
    assert_equal {"id" => 1, "name" => "lolychee"}, User.first.slice(:id, :name)
    
  • Rails 新建的表在 Git 回滚后每次 migrate 时反复出现 at April 19, 2015

    你数据库里那个表没删掉,schema.rb 是按当前数据库结构生成的。

  • # 抵制浪费时间的编码面试 # 用一个开源的方式来说明 at April 18, 2015

    #4 楼 @billy 是可以写 class, module 的,他的代码测试结果是

    21 Passed
    3 Failed
    0 Errors
    
  • # 抵制浪费时间的编码面试 # 用一个开源的方式来说明 at April 18, 2015

    #6 楼 @mvj3 我不在那个帖子里回你了 https://ruby-china.org/topics/25179#reply3

    如果你觉得你的答案更有技巧和扩展性请先通过测试再说,你现在代码绝对通不过测试,原因我想请你先自己找一下。

  • RMagick undefined method `include' for at April 12, 2015

    你代码贴的不全,我猜一下,完整的代码应该是这样:

    class GalleriesController
    
      def ajax_getGalleryMiddleImage
        require 'RMagick'
        include Magick
    
        #...
      end
    
    end
    

    按照这样的代码来分析: 首先 include 是 Module 的实例方法,只能这样用:

    class GalleriesController
      include Magick
    
    end
    

    然后,你确定你要把 Magick 作为模块引入 GalleriesController 里?我对这个库不太了解,但是我查了下,不需要引入吧。

  • 重新思考找回忘记密码解决方法 at April 10, 2015

    密码重置有个基本性原则,就是一个链接只能重置一次,用完作废。你的例子里设定了两天才过期,这两天用这个链接真是想怎么重置怎么重置。

    reset_password_at 还是要的,如果一定要追求不添加字段,用 updated_at 代替应该也可以吧。

  • 如何解决测试代码重复的问题? at March 05, 2015

    大概看了一下,你这些重复的代码都是和权限有关的对吧。给你分享下我现在用的办法:

    # spec/support/shared_examples_for_authorized.rb
    
    require "rails_helper"
    
    RSpec.shared_examples "authorized" do |request_proc|
    
      let(:pass) { [create(:user)] }
      let(:deny) { [nil] }
      let(:status) { :unauthorized }
    
      it "返回其他的 http status" do
        pass.each do |user|
          user ? controller.sign_in(user) : controller.sign_out
          instance_exec(&request_proc)
          expect(response).not_to have_http_status(status)
        end
      end
    
      it "返回指定的 http status" do
        deny.each do |user|
          user ? controller.sign_in(user) : controller.sign_out
          instance_exec(&request_proc)
          expect(response).to have_http_status(status)
        end
      end
    
    end
    
    
    # spec/controllers/comments_controller_spec.rb
    
    RSpec.describe CommentsController, :type => :controller do
    
      #...
    
      describe "GET edit" do
        it_behaves_like "authorized", -> { get :edit, {id: create(:comment, author: current_user).to_param} }
        it_behaves_like "authorized", -> { get :edit, {id: create(:comment, author: current_user).to_param} } do
          let(:status)  { :forbidden }
          let(:pass)    { [current_user, create(:user, :admin)] }
          let(:deny)    { [create(:user)] }
        end
    
        it "assigns the requested comment as @comment" do
          comment = create(:comment, author: current_user)
          get :edit, {id: comment.to_param}, valid_session
          expect(assigns(:comment)).to eq(comment)
        end
      end
    
      #...
    
    end
    

    不知道你能不能看明白?

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