草庐IT

current_count

全部标签

ruby-on-rails - 如何每第三个 missed_day 重新启动 current_level(3 次罢工!)?

例如,如果用户处于第4级:第50天,他将被推回到第45天。casen_dayswhen0..9#Wouldgobackto01when10..24#Backto102when25..44#Backto253when45..69#Backto454when70..99#Backto705else"Mastery"end然后假设他再次返回,这次是第68天,如果他再次勾选3missed_days,他将再次被推回到>第45天:_form(如上图所示):"class="habit-id">Missed:"class="level-id">Level:0,{class:"habit-check"}

ruby-on-rails - Rails 上的 Ruby + Active_Record : How do I include child counts in result?

我想出了如何通过在文件夹模型中创建as_json方法将子项包含在to_json结果中。defas_json(options={})super(options.merge(:include=>{:children=>{}}))end上面的代码给了我一个child的列表,但我想要的是包括计数而不是child的列表。我还想将其过滤为仅“活跃”的child。我似乎想不出一个有效的方法来做到这一点。我正在使用以下代码返回文件夹列表。defindex@folders=Folder.all(:order=>"Name")respond_with(@folders)do|format|format.j

ruby-on-rails - current_page 正在使用语言环境参数,但没有它们就无法工作

我正在使用current_page将事件类分配给导航菜单,但我认为我不了解current_page的工作原理。示例代码如下。home_controller.rbclassHomeControllerapplication.html.erb当在localhost:3000/menu时,application.html.erb打印false。当在localhost:3000/menu?locale=en时,application.html.erb打印true。在localhost:3000/menu?locale=ko中时,application.html.erb也打印true。无论loc

ruby-on-rails - correct_user 不使用 current_user?

我希望未登录的人能够看到展示页面。现在他们收到current_user的NoMethodError错误。defshow@correct_user=current_user.challenges.find_by(id:params[:id])endsessions_helper#Returnsthecurrentlogged-inuser(ifany).defcurrent_userif(user_id=session[:user_id])@current_user||=User.find_by(id:user_id)elsif(user_id=cookies.signed[:user_

ruby-on-rails - current_user.present 和 if user_signed_in 有什么区别?

正在设计并想知道两者之间的区别是什么和 最佳答案 不,实际上没有区别。查看user_signed_in?的元编程实现:def#{mapping}_signed_in?!!current_#{mapping}end当针对User模型进行身份验证时,这解析为:defuser_signed_in?!!current_userend注意:如果current_user为nil或false,则!!current_user返回true>。这与present?完全相同 关于ruby-on-rails-c

ruby - 什么是哈希#count?

当我注意到这个时,我正在玩IRB(Ruby2.5.1):irb(main):020:0>h=>{3=>4,:aaa=>false}irb(main):021:0>h.count=>2然而,这个方法在Rubydocs中不存在。.快速测试表明hsh.count给出与hsh.keys.count相同的结果,并且Hash.ancestors包含可枚举。Hash#count到底是什么? 最佳答案 您似乎已经了解了大部分内容...它是Enumerable#count.从技术上讲,hsh.keys.count正在计算键数,hsh.count正在计

ruby - 使用 RSpec 测试 @current_user 方法

这几天我一直在尝试这样做,但我无法弄清楚。我的Controller中有以下代码:@some_object=@current_user.some_method在我的规范中,我想在那个方法上附加一个should_receiveHook,但我无法让它工作。我已经尝试了所有这些,但都没有用:assigns[:current_user].should_receive(:some_method).at_least(:once)#expected1,got0User.should_receive(:some_method).at_least(:once)#expected1,got0正确的测试方法是

ruby-on-rails - 在 Rails 的 API 包装器中使用 Thread.current 的替代方法

我开发了一个应用程序,允许我们的客户创建他们自己的成员(member)保护网站。然后,我的应用程序连接到外部API服务(客户特定的api_key/api_url)以将数据同步/更新/添加到该其他服务。好吧,我已经为到目前为止一直有效的其他服务编写了一个API包装器。但是,我现在看到连接为零的情况非常随机。这是我目前使用连接的方式:我有一个xml/rpc连接类classApiConnectionattr_accessor:api_url,:api_key,:retry_countdefinitialize(url,key)@api_url=url@api_key=key@retry_co

ruby 鞋子 : counting the amount of times a value occurs in an Array

我正在使用Shoes在Ruby中制作Yahtzee游戏当我点击按钮“Two”时,代码应该计算值2出现在数组中。对于出现的值2的每个实例,分数增加2。此代码适用于特定数量的案例,但适用于其他情况,例如@array=[2,1,2,2,3]#数组中有三个2所以分数应该是6,但我的代码却返回4...为什么?button"twos"do@array.each_with_indexdo|value,index|if(@array[index]==2)@score=@score+2@points=@score+2end#ifend#loopend#button 最佳答案

ruby-on-rails - 如何将 current_user 传递给 Sidekiq 的 Worker

我正在尝试将current_user或User.find(1)传递给工作模块,但在sidekiq的仪表板中出现错误(localhost:3000/sidekiq/retries):NoMethodError:undefinedmethod`supports'for"#":String注意:我的关系还好,即:u=User.find(1)u.supports#=>[]supports_controller.rb:defcreate@user=current_userProjectsWorker.perform_async(@user)...endapp/workers/projects_w