草庐IT

do_something_with_hex

全部标签

ruby-on-rails - Rails 上的 ruby : How to have multiple submit buttons going to different methods (maybe with with_action? )

这个问题在这里已经有了答案:HowdoIcreatemultiplesubmitbuttonsforthesameforminRails?(7个答案)关闭9年前。所以..'save'%>'library'%>然后在我的Controller中:with_actiondo|a|a.savedoenda.librarydoendend问题是只有一个操作被调用...两个submit_tags调用相同的操作...知道为什么吗?或者我如何获得两个按钮以将表单提交给两种不同的方法?

ruby-on-rails - rails : Using form fields that are unassociated with a model in validations

在RubyonRails应用程序中,我尝试使用来自与验证模型无关的字段的信息。这里以模型的一部分为例(整个模型有点大):classScorecard那么如何从模型中访问params呢? 最佳答案 不要让参数偷偷靠近模型。在这种情况下没有Controller的意义。相反,从Railscasts查看这一集它讨论了不进入数据库但仍可用于验证的虚拟属性。虚拟属性不需要相应的模型属性。定义类的局部属性,例如保存状态的@no_fairways。classScoreCard现在在你的表单中,你可以写:

ruby-on-rails - 在 respond_with 散列中包含一个虚拟属性

我试图在respond_toJSON散列中包含一个虚拟属性/方法。模型(employee.rb)attr_reader:my_methoddefmy_methodreturn"foobar"endController(employees_controller.rb)respond_to:jsondefindex@employees=Employee.allrespond_with(:data=>@employees,:total=>Employee.all.count)end重要的是,我将“data”作为“employees”集合的json根,并将“total”包含在散列中。这很好用,

ruby - Rails3 : combine scope with OR

我需要将名称范围与or运算符组合...像这样的东西:classProduct谢谢 最佳答案 来自AreldocumentationTheORoperatorisnotyetsupported.Itwillworklikethis:users.where(users[:name].eq('bob').or(users[:age].lt(25)))ThisRailsCast向您展示如何使用.or运算符。但是,当您拥有ActiveRecord::Relation实例时,它可以与Arel对象一起使用。您可以使用Product.name_a.

ruby-on-rails - map(& :name) do in this Ruby code? 是什么意思

为了更好地理解Ruby,我在网上冲浪时遇到了这段代码:require'rubygems'require'activeresource'ActiveResource::Base.logger=Logger.new("#{File.dirname(__FILE__)}/events.log")classEvent"Shortesteventevar!",:starts_at=>1.second.ago,:capacity=>25,:price=>10.00)e.destroy我特别感兴趣的是events.map(&:name)是如何工作的?我看到events是一个数组,因此它调用了它的ma

ruby - `sort_by' : comparison of Array with Array failed (no nil data)

classCustomSorterattr_accessor:start_date,:availabledefinitialize(start_date,available)@start_date=Time.mktime(*start_date.split('-'))@available=availableendendcs1=CustomSorter.new('2015-08-01',2)cs2=CustomSorter.new('2015-08-02',1)cs3=CustomSorter.new('2016-01-01',1)cs4=CustomSorter.new('2015-0

ruby - 导轨 : How do I require NumberHelper and make it work?

我正在尝试编写一个简单的Sinatra东西,但我需要操作包中的ActionView::Helpers::NumberHelper。http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html问题是,如何安装和使用它?irb(main):001:0>require'action_view/helpers/number_helper'irb(main):002:0>number_with_precision(1)NoMethodError:undefinedmethod`number_with_precisi

ruby-on-rails - 缺少 `secret_token` 和 `secret_key_base` - Rails 4.2.0 with RVM

最近,我从Git中提取了一个存储库。启动服务器后,我收到以下消息:缺少secret_token和secret_key_base。这可能是因为我在我的.gitignore中包含了secrets.yml。我目前的设置Ubuntu14.04ruby2.2.0p0rails4.2.0rvm1.26.11本地服务器(非远程)开发环境许多在线资源指出我必须使用rakesecret生成新key并将其添加到secrets.yml中。将key放在secrets.yml中并重新启动Rails服务器不起作用。Edited:Addedcontentsofsecrets.ymlbelow.-04/30/159:

ruby-on-rails - rails : Where does new_*something*_path variable get set up?

我为“消息”创建了一个脚手架,并且new_message_path和edit_message_path(用于link_to's)都已设置,但现在我已经创建了app/views/messages/sent.html.erb,我想做类似的内容,但我不知道该怎么做。我明白了undefinedlocalvariableormethod`sent_message_path'for# 最佳答案 这些方法是在定义路由时自动创建的,对于RESTful路由,它们遵循可预测的约定。运行“rakeroutes”是查看生成的所有路由的有用方法。我建议您阅读

ruby-on-rails - 使用设计上下文的 "super"和 "super do |u|"之间的区别

好吧,我想我得到了什么superdoes独立的。基本上在设计中,如果Users::RegistrationsController,然后在任何行动中,都有一个super将首先调用父级中相同命名操作的逻辑Devise::RegistrationsController,然后调用你写的内容。换句话说...classDevise::RegistrationsControllerdefnewputs"thisisintheparentcontroller"endendclassUsers::RegistrationsController"thisisintheparentcontroller"#=