草庐IT

Rails-Webpacker

全部标签

ruby-on-rails - 使用带有哈希的 def_delegate

我知道Forwardable#def_delegate如何与对象的方法一起工作,但是是否有类似的方法将方法名称转发到哈希键。喜欢:hash={some_value:42,other_value:31415}def_delegate:hash,:some_value,:other_value调用object.some_value应该返回42PS:def和classeval是一种方法,但是有更好的方法吗? 最佳答案 这对OpenStruct来说是件好事,它基本上将哈希包装在一个对象中。2.2.1:001>require'ostruct'

ruby-on-rails - 您已经激活了 spring 1.3.6,但是您的 Gemfile 需要 spring 1.3.3。 ( gem ::加载错误)

我今天遇到了同样的问题,有一个建议:在您的命令前添加bundleexec可能会解决此问题。前置bundleexec没有帮助(我已经这样做了)。springstop和springrestart没有帮助。我需要做的:bundleupdatespring这对我有用。在之前的gemlock文件中使用spring版本是否有更好的解决方案? 最佳答案 我删除gemfile.lock并运行bundle通常会清除一切。否则只需从Gemfile中删除gem"spring"并运行bundle 关于ruby-

ruby-on-rails - 30.seconds.ago 之类的东西是如何实现的?

我发现了这个问题here.并且非常想知道如何在Rails中实现类似30.seconds.ago的技术解释。方法链?Numeric用法如下:http://api.rubyonrails.org/classes/Numeric.html#method-i-seconds.还有什么? 最佳答案 Here是seconds的实现:defsecondsActiveSupport::Duration.new(self,[[:seconds,self]])end并且,here是ago的实现:#CalculatesanewTimeorDatethat

ruby-on-rails - 在 El Capitan 上安装 Rails 时出现 -lgmp 错误的库未找到(Mac OS 10.11.1 (15B42))

在使用Rubyv2.2.2的ElCapitan(MacOSX10.11.1)上安装Rails时,出现以下错误:ERROR:Errorinstallingnokogiri:ERROR:Failedtobuildgemnativeextension./Users/jon/.rvm/rubies/ruby-2.2.2/bin/ruby-r./siteconf20151117-26799-ux15fd.rbextconf.rb--use-system-librariescheckingiftheCcompileraccepts...***extconf.rbfailed***Couldnotc

ruby-on-rails - `require' : No such file to load -- test_helper (LoadError)

当我在生产模式下运行我的Rails应用程序时出现以下错误,但是当我在开发模式下运行我的应用程序时它工作正常。我可以在生产模式下使用任何Gem吗?`require':Nosuchfiletoload--test_helper(LoadError)以下是完整的代码轨迹:/home/nyros/.rvm/gems/ruby-2.2.0@dfl/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:229:in`require':Nosuchfiletoload--test_helper(LoadError)from/home/

ruby-on-rails - (Rails 新手)rails 服务器错误 : cannot load such file -- bootsnap/setup (LoadError)

我已经将我的JS运行时安装为Node,并且我已经更新了bundle并安装了bundle,但我仍然收到此错误:/Users/macbook/Documents/Studies/Coding/the_back_end/the_odin_project/odin_on_rails/hello_app/config/boot.rb:4:in`require':cannotloadsuchfile--bootsnap/setup(LoadError)from/Users/macbook/Documents/Studies/Coding/the_back_end/the_odin_project/

ruby-on-rails - 带有 Memcache 的 Rails 返回错误的缓存对象?

我有一个相当大的Rails应用程序,它在单独的服务器上使用memcached作为其缓存存储。问题是我在生产环境中随机出现错误,似乎表明memcached正在返回不正确的对象。例子:在这个例子中,current_site是一个辅助方法,它访问Site模型上的一个方法,该模型使用Rails.cache来缓存模型ActionView::TemplateErrorinListingsController#editundefinedmethod`settings'for#Online#12ofapp/views/layouts/site.html.erb9:10:11:12:13:"/>14:1

ruby-on-rails - 如何迭代 ActiveRecord 属性,包括 attr_accessor 方法

我到处寻找优雅的解决方案。本质问题似乎是映射到数据库列的ActiveRecord属性在ActiveRecord::Base中的处理方式与attr_accessor方法完全不同。我想做这样的事情:model.attribute_names.eachdo|name|#dostuffend在某种程度上也包括attr_accessor字段,但不包括任何其他实例方法。我知道这不是内置的,但最优雅的方法是什么? 最佳答案 你无法真正解决这个问题。您可以近似破解,但它永远不会很好地工作。model.attribute_names应该给你所有的Ac

ruby-on-rails - 在 Rails 中,如何使用字符串数组实现 HTML 选择菜单?

我有一个FinancialDocument#document_type模型属性。我想让用户从由字符串数组填充的HTML选择菜单中选择文档类型...doctypes=['Invoice','Packingslip','Other']对于每个选项,显示的标签和返回的值都是相同的。我查看了select和collection_select助手,但它们似乎适合选择子模型,而不仅仅是一个String值。我找不到如何让它们达到我的目的。这是我正在尝试的方法(我使用的是Haml,而不是Erb)...form_for(@financial_document)do|f|-doctypes=['Invoic

ruby-on-rails - 如何使用 AuthLogic 模拟用户

我需要能够在没有解密密码的情况下创建UserSession。我该怎么做?我目前的解决方法是:在user.rb中defvalid_crypted_or_non_crypted_password?(password)valid_password?(password)||password==crypted_passwordend在user_session.rb中verify_password_method:valid_crypted_or_non_crypted_password?登录UserSession.create(:login=>u.login,:password=>u.crypte