Rails将params / arguments传递给ActiveRecord回调函数

我在其中一个AR型号中有以下代码段: after_update :cache_bust 模型中的cache_bust方法接受一个参数(布尔值),默认情况下将其值设置为false 。 如何从上面定义的ActiveRecord回调中将true传递给模型中的this方法呢? 例如: after_update :cache_bust(true)不起作用,因为它是一个符号,我怎样才能将true传递给方法的参数?

find_spec_for_exe’:找不到gem bundler(> = 0.a)(Gem :: GemNotFoundException)

我使用sudo bundle install ,这可能是问题的原因? 我现在有: gem -v 2.6.14 ruby -v ruby 2.3.5p376(2017-09-14修订版59905)[x86_64-darwin15] jekyll -v jekyll 3.6.2 bundle -v Bundler version 1.16.0.pre.3 尝试运行bundle exec jekyll serve或jekyll serve时,我收到以下错误 /Users/myusername/.rvm/rubies/ruby-2.3.5/lib/ruby/site_ruby/2.3.0/rubygems.rb:271:in find_spec_for_exe’: can’t find gem bundler (>= 0.a) (Gem::GemNotFoundException) from /Users/myusername/.rvm/rubies/ruby-2.3.5/lib/ruby/site_ruby/2.3.0/rubygems.rb:299:in activate_bin_path’中的find_spec_for_exe’: can’t find gem bundler (>= 0.a) (Gem::GemNotFoundException) from /Users/myusername/.rvm/rubies/ruby-2.3.5/lib/ruby/site_ruby/2.3.0/rubygems.rb:299:in gems / ruby​​-2.3.5 / bin / bundle:23:in ‘ […]

在查询上使用Elasticsearch进行方面的麻烦

在我的查询中添加一个术语而不是filter时,我得到了0个方面。 仅供参考我正在使用Ruby的轮胎gem。 这是我的模型代码及其映射: class Property ‘snowball’, :boost => 100 indexes :description indexes :tags, type: ‘object’, properties: { name: { type: ‘multi_field’, fields: { name: { type: ‘string’, analyzer: ‘snowball’ }, exact: { type: ‘string’, index: ‘not_analyzed’ } } } } end def to_indexed_json to_json( include: { tags: { only: [:name] }, }) end 然后是搜索方法 def […]

Rails删除方法它不起作用

好吧,我在Ruby on Rails中遇到删除方法存在这样的问题,我想,我尝试了所有我读过的但是它不起作用也许你gusy可以帮助修复差距。 当我点击链接它重定向到患者/ 1?确认=是+你+确定%3F&方法=删除 但是从我的chrome控制台收到一条消息Uncaught SyntaxError:Unexpected token =:3000 / assets / application.js?body = 1:13属于 =需要jquery 我的代码如下: patients.controller def show @patient = Patient.find(params[:id]) end def new @patient = Patient.new @patients = Patient.find(:all) end def create @patient = Patient.new(params[:patient]) if @patient.save redirect_to new_patient_path end end def edit @patient = Patient.find(params[:id]) end def update @patient = Patient.find(params[:id]) […]

Actionmailer不使用rails 3传递邮件

我正在尝试创建一个应用程序,在用户注册时发送电子邮件。 我在config / application.rb文件中输入了gmail的smtp设置,邮件function看起来像 mail(:to => “me@me.com”, :subject => “Mail!”, :from => “another@me.com”, :content_type => “text/html”) 现在,当我看到日志时,我看到它说邮件已被发送,但我从未收到任何邮件… 另外,当我调用邮件传递函数Emails.signed(@user).deliver ,表单页面不会重定向,但如果我注释掉发送代码的电子邮件,它就可以工作 Emails.signed(@user).deliver 要么 mail(:to => “me@me.com”, :subject => “Mail!”, :from => “another@me.com”, :content_type => “text/html”) 谢谢 :) 编辑:development.rb App::Application.configure do # Settings specified here will take precedence over those in config/environment.rb # In the development environment your […]

在Rails中获取STI类基类的路由

假设我有一个带有3个模型的rails应用程序,Person,Place和Thing。 假设Thing使用单表inheritance,因此有FancyThing和ScaryThing子类。 然后是使用map.resources :people, :places, :things定义的路由map.resources :people, :places, :things 。 所以没有FancyThings和ScaryThings的控制器,ThingsController处理任何类型。 现在说我需要有代码,显示任何有链接的列表。 如果我在我的视图中有这个代码: 如果item是Person或Place,这可以正常工作, polymorphic_path负责生成正确的路由。 但是如果item是FancyThing或ScaryThing,这会爆炸,因为它会尝试使用没有路径的fancy_thing_path 。 我想以某种方式使它使用thing_path 。 理想情况下,Thing和/或其子类上会有一个方法以某种方式指示子类应该使用基类来生成路由。 有一个优雅的解决方案吗?

为什么机架响应体不是字符串?

他们的文档中的经典hello world示例, class HelloWorld def call(env) return [200, {}, [“Hello world!”]] end end 我的问题是为什么第三个值是[Hello world!“],而不是”Hello world“?来自他们的doc, Body必须响应每个,并且只能产生String值。 Body本身不应该是String的实例,因为这将在Ruby 1.9中破坏。 为什么身体需要对每个人做出回应? 在什么情况下重要?

Ruby使用rbenv在OSX Lion上失败

我一直在尝试在我的Lion机器上安装Ruby(OSX 10.7.3)。 我安装了rbenv和ruby-build,我试图在我的机器上获得1.9.2-p290。 以下是安装失败的输出: $ rbenv install 1.9.2-p290 Downloading http://pyyaml.org/download/libyaml/yaml-0.1.4.tar.gz… Installing yaml-0.1.4… Installed yaml-0.1.4 to /Users/CSC/.rbenv/versions/1.9.2-p290 Downloading http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.2-p290.tar.gz… Installing ruby-1.9.2-p290… BUILD FAILED Inspect or clean up the working tree at /var/folders/vm/gcqcld015jx025m7qgtxvw0m0000gn/T/ruby-build.20120301140751.6239 Results logged to /var/folders/vm/gcqcld015jx025m7qgtxvw0m0000gn/T/ruby-build.20120301140751.6239.log Last 10 log lines: /usr/bin/gcc-4.2 -I. -I../../../.ext/include/x86_64-darwin11.3.0 -I../../.././include -I../../.././ext/racc/cparse -DRUBY_EXTCONF_H=\”extconf.h\” -I’/Users/SamCouch/.rbenv/versions/1.9.2-p290/include’ -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -fno-common -O3 -ggdb -Wextra -Wno-unused-parameter -Wno-parentheses -Wpointer-arith […]

模块中的实例方法

请考虑以下代码: module ModName def aux puts ‘aux’ end end 如果我们用class替换module ,我们可以执行以下操作: ModName.new.aux 但是,模块不能被实例化。 有没有办法在模块上调用aux方法?

如何让Nokogiri解析并返回XML文档?

这是一些奇怪的例子: #!/usr/bin/ruby require ‘rubygems’ require ‘open-uri’ require ‘nokogiri’ print “without read: “, Nokogiri(open(‘http://weblog.rubyonrails.org/’)).class, “\n” print “with read: “, Nokogiri(open(‘http://weblog.rubyonrails.org/’).read).class, “\n” 运行此返回: without read: Nokogiri::XML::Document with read: Nokogiri::HTML::Document 没有read返回XML,并且它是HTML? 网页被定义为“XHTML过渡”,所以起初我认为Nokogiri必须从流中读取OpenURI的“内容类型”,但返回’text/html’ : (rdb:1) doc = open((‘http://weblog.rubyonrails.org/’)) (rdb:1) doc.content_type “text/html” 这是服务器返回的内容。 所以,现在我想弄清楚为什么Nokogiri会返回两个不同的值。 它似乎不是解析文本并使用启发式方法来确定内容是HTML还是XML。 该页面指向的ATOM提要也发生了同样的事情: (rdb:1) doc = Nokogiri.parse(open(‘http://feeds.feedburner.com/RidingRails’)) (rdb:1) doc.class Nokogiri::XML::Document (rdb:1) doc = Nokogiri.parse(open(‘http://feeds.feedburner.com/RidingRails’).read) (rdb:1) doc.class Nokogiri::HTML::Document […]