草庐IT

ruby-on-rails - Rails 4 嵌套属性不保存

我似乎无法将嵌套属性保存到数据库中。我正在使用Rails4。这是我的模型:classAnswer这是Controller:defcreate@survey=Survey.new(survey_params)respond_todo|format|if@survey.saveformat.html{redirect_to@survey,notice:'Surveywassuccessfullycreated.'}format.json{renderaction:'show',status::created,location:@survey}elseformat.html{renderac

ruby-on-rails - Rails 使用逻辑运算符搜索 ActiveRecord

我想知道在Rails中解析文本查询的最佳方法是什么,以允许用户包含逻辑运算符?我希望用户能够输入其中任何一个,或一些等效的:#searchingpartialtextinemails,justforexample#queryA"jonANDgmail"#=>["jonsmith@gmail.com"]#queryB"jonORgmail"#=>["jonsmith@gmail.com","sarahcalaway@gmail.com"]#queryC"jonANDgmailANDsmith"#=>["jonsmith@gmail.com"]理想情况下,我们可以使用括号来指示操作顺序,从

ruby-on-rails - 开始对 Ruby Rails 网站进行单元和功能测试的最佳方法是什么?

我正在测试一个RubyRails网站,想开始进行单元和功能测试。 最佳答案 Cucumber和RSpec值得一看。他们鼓励在behaviour-driven中进行测试,基于示例的样式。RSpec是一个用于单元级测试的库:describe"hello_world"it"shouldsayhellototheworld"do#RSpeccomeswithitsownmock-objectframeworkbuiltin,#thoughitletsyouuseothersifyoupreferworld=mock("World",:pop

ruby-on-rails - 为什么我会收到此 'can' t modify frozen hash' 错误?

我有一个Person模型和一个Item模型。一个人有很多元素,一个元素属于一个人。在此代码中,我需要删除一个人的现有项目,并根据参数(这是一个哈希数组)创建新项目。然后,我需要根据项目的其他字段之一更新项目的字段之一。@person=Person.find(params["id"])@person.person_items.eachdo|q|q.destroyendperson_items_from_param=ActiveSupport::JSON.decode(params["person_items"])person_items_from_param.eachdo|pi|@per

ruby-on-rails - Prawn pdf附件在邮件中

在我的Rails应用程序中,我尝试将发票附加到电子邮件中:definvoice(invoice)attachment:content_disposition=>"attachment",:body=>InvoicePdf.new(invoice),:content_type=>"application/pdf",:filename=>'invoice.pdf'mail(:to=>@user.email,:subject=>"YourInvoice")endInvoicePdf是一个PrawnPDF文档:classInvoicePdf我在电子邮件中没有收到附件。我究竟做错了什么?任何提示

ruby-on-rails - 无法验证 CSRF token 真实性 Rails 4.1

我正在开发一个简单的网站,让管理员提出问题并让用户解决问题。我在管理部分使用ActiveAdmin,在用户解决部分使用简单的AJAX调用。起初尝试通过ActiveAdmin::Devise登录是成功的,但无法退出。我删除了所有cookie,从那时起我无法在没有CSRFtoken真实性异常的情况下进行POST操作。我的application.html.erb头部有正确的meta_tags,声明为jquery_ujs(其他线程说这是一个常见问题),并且在两个POST操作中都存在真实性token。我什至尝试通过skip_before_filter:verify_authenticity_to

ruby-on-rails - 无法加载此类文件 — ruby​​gems.rb

要打开这个项目(revel),我需要使用ruby​​-2.1.2。我同时安装了ruby​​-2.1.0和ruby​​-2.1.2,但我认为2.1.2版本由于某种原因已损坏,我似乎无法使用它。|~/desktop/ix/projects/revel>rvmuse2.1.2Using/Users/lianoosh/.rvm/gems/ruby-2.1.2:1:in`require':cannotloadsuchfile--rubygems.rb(LoadError)from:1:in`'|~/desktop/ix/projects/revel>gemlist:1:in`require':c

ruby-on-rails - Rails,activerecord : self[:attribute] vs self. 属性

在Rails中访问事件记录列/属性时,使用self[:attribute]与self.attribute有什么区别?这会影响getter和setter吗? 最佳答案 它们都只是获取属性的方法-它们都只是getter。self.attribtue是一个更“传统”的getter,而self[:attribute]基本上只是[]方法。在使用两者之间切换不会产生任何影响。我建议只使用self.attribute方法,因为它在语法上更好。但是,当其他内容覆盖self.attribute方法时,使用self[:attribute]会派上用场。例

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

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

ruby-on-rails - 对象集合的 Rails 模型类方法

我在编写用于ActiveRecord集合的类方法时遇到问题对象。我在过去几个小时内两次遇到这个问题,这似乎是一个简单的问题,所以我知道我遗漏了一些东西,但我无法在其他地方找到答案。例子:classOrder{where('order_date>?',DateTime.now.beginning_of_month.utc)}defself.first_order_countmap(&:first_for_customer?).count(true)enddeffirst_for_customer?self==customer.orders.first#thisself==bitseems