Tag: ruby

将变量从Javascript传递到Ruby on Rails

在rails中你可以这样做: var name = ; alert(name); //prints the name of the user 你可以反过来这样做 var name = “John”; = name; 显然这不起作用,但是出于示例的目的 关键是,你能否将javascript变量的值传递给rails中的ruby变量

如何在密码更改时覆盖设计错误消息

如何自定义错误消息被覆盖的divise密码控制器? class PasswordsController home_path else binding.pry flash[:devise_password_error] = (resource.errors.map do |key, value| value.capitalize end).flatten.join(‘|’) redirect_to home_path and return end end def edit self.resource = resource_class.new resource.reset_password_token = params[:reset_password_token] end end resource.errors在此方法中可用,但它包含默认消息,例如Email not found Email can’t be blank并且Email can’t be blank 。 我需要自定义此消息。 我试图删除:validatable从我的用户模型中:validatable并添加自定义validation器,但这仅适用于从Devise :: RegistrationsController派生的自定义注册控制器,而不适用于自定义密码控制器。 有什么解决方案吗?

编码:: UndefinedConversionError:“\ xE4”从ASCII-8BIT到UTF-8

我试图用Net::HTTP获取这个CSV文件 。 File.open(file, “w:UTF-8”) do |f| content = Net::HTTP.get_response(URI.parse(url)).body f.write(content) end 再次读取我的本地csv文件后,我得到了一些奇怪的输出。 Nationalit \ xE4t;改0-5 我试图将其编码为UTF-8,但是Encoding::UndefinedConversionError: “\xE4” from ASCII-8BIT to UTF-8收到错误Encoding::UndefinedConversionError: “\xE4” from ASCII-8BIT to UTF-8 rchardet gem告诉我内容是ISO-8859-2 。 但转换为UTF-8将无法正常工作。 在正常的Texteditor中打开后,我看到它是正常编码的。

时间gt查询无法在轨道上使用mongoid和ruby

我在轨道上使用mongoid和ruby。 我使用mongoid存储created_at时间 include Mongoid::Timestamps 所以,假设我有3个post就是这样创建的 {“post”:”1″,”created_at”: “2014-03-25 13:04:43”} {“post”:”2″,”created_at”: “2014-03-25 13:04:44”} {“post”:”3″,”created_at”: “2014-03-25 13:04:45”} 现在我想获得在{“post”:”2″}之后创建的所有post,即”2014-03-25 13:04:44″ ,这样只返回{“post”:”3″} Model.where(:created_at.gt => “2014-03-25 13:04:44”) 但是在上面的查询中它返回{“post”:”2″}和{“post”:”3″} ,所以{“post”:”2″}不应该在那里。 此.lt查询按预期工作。 任何想法为什么会这样? 提前致谢。

在Link_to中传递参数

我想通过Rails中的link_to方法传递参数。 我知道有一种方法可以通过URL来实现,但我真的不想那样做。 有没有办法通过链接传递参数而不将其添加到链接本身? 我知道在PHP中你可以发布然后使用post变量检索该值。 Rails中有类似的东西吗?

在Ruby on Rails上载文件

我有一个Web应用程序,需要从用户上传文件并将其上传到远程服务器。 我可以通过file_field从用户到服务器的输入很好,但似乎无法计算从服务器上传到远程的下一步。 Net :: HTTP没有开箱即用的多部分表单,我也找不到另一个好的解决方案。 我需要的东西可以让我从用户 – >服务器 – >远程而不是去用户 – >远程。 有没有人成功过这样做过?

为什么`method =`与其他方法一样处理?

请考虑以下代码段: class Example def my_attr=(value) @_my_attr = value @_my_attr * 3 end end 我希望表达式Example.new.my_attr = 5返回15 ,但结果certificate是错误的。 即使我明确调用=方法,也始终返回原始返回值: Example.new.my_attr = 5 # => 5 Example.new.my_attr=(5) # => 5 Ruby如何以及为什么这样做? Ruby是否处理以特殊结尾的方法,还是其他机制? 我想这排除了链接=方法的返回值,对吗? 有没有办法让Ruby的行为不同,或者这是怎么回事? 更新:感谢@jeffgran: Example.new.send(:my_attr=, 5) # => 15 这是一种解决方法,但在另一个层面上更令人困惑,因为这意味着send显然并不总是等同于直接调用方法的行为。

如何避免来自RubyGems的弃用消息?

在gem update –system ,当我做一些与rubygems相关的事情时,我会收到以下消息: 注意:不推荐使用Gem :: Specification#default_executable =。 它将在2011-10-01之后删除。 Gem :: Specification#default_executable =从/usr/lib/ruby/gems/1.8/specifications/rubygems-update-1.7.2.gemspec:11调用。 我怎么能避免呢? 我试图删除rubygems并重新安装,但这并没有解决它。

切换到黄瓜,水豚的弹出窗口

在RSpec我可以使用这样的代码切换到弹出窗口, 链接 ,我怎么能在Cucumber步骤中做这样的事情? login_window = page.driver.find_window(‘PPA_identity_window’) main_window = page.driver.find_window(”) # We use this to execute the next instructions in the popup window page.within_window(login_window) do #Normally fill in the form and log in fill_in ’email’, :with => “” fill_in ‘password’, :with => “” click_button ‘Log In’ end

在Ruby中使用Marshal :: dump进行对象序列化时如何写入文件

假设我有类Line的对象行 : class Line def initialize point1, point2 @p1 = point1 @p2 = point2 end end line = Line.new … 如何对行对象进行二进制序列化? 我尝试过: data = Marshal::dump(line, “path/to/still/unexisting/file”) 但它创建了文件,并没有添加任何东西。 我读了Class:IO文档,但我无法真正理解它。