Tag: ruby on rails

使用Hash #fetch优雅地处理错误的JSON

我正在尝试从这个JSON API中获取一些产品,所以我可以在我的视图中显示它们,使用Hash#fetch #fetch优雅地处理坏JSON,如果我得到nil,则声明一个默认值。 但为什么我得到: can’t convert String into Integer 或者如果我将@json_text设置为{} : undefined method ‘fetch’ for `nil:NilClass` 直播应用: http : //runnable.com/U-QEWAnEtDZTdI_g/gracefully-handle-bad-json class MainController < ApplicationController require 'hashie' def index @json_text = <<END { "products": [] } END hashes = JSON.parse(@json_text) mashes = Hashie::Mash.new(hashes) products = [] mashes.products.fetch('products', []).each do |mash| puts "YOLO" end end end

bundle无法加载这样的文件bundler-1.17.1 / exe / bundle

我遇到了FileUtils的问题,我不得不卸载gem。 在这之后,我做了gem update –default并且一些gem已经更新。 这解决了FileUtils的问题,但现在,捆绑包已经死了。 如果我尝试bundle info xxx我收到此错误: Traceback (most recent call last): 1: from /Users/robin/.gem/ruby/2.5.1/bin/bundle:23:in `’ /Users/robin/.gem/ruby/2.5.1/bin/bundle:23:in `load’: cannot load such file — /Users/robin/.rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/bundler-1.17.1/exe/bundle (LoadError) ruby 2.5.1p57 gem 2.7.7 Rails 5.2.1 我正在使用chruby,我安装了Ruby 2.5.0和2.5.1。 我试图卸载bundler并重新安装它,但这不起作用。 现在,即使我尝试使用gem install bundler , ~/.rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/文件夹中也没有目录。 但是,我安装它时没有错误: gem install bundler Successfully installed bundler-1.17.1 Parsing documentation for bundler-1.17.1 Done installing documentation for […]

rails中的Active_Model错误

我回到了我正在处理的rails项目,命令“rails s”开始失败。 终端建议我运行捆绑安装。 当我这样做时,我收到以下消息: Bundler could not find compatible versions for gem “activemodel”: In Gemfile: protected_attributes (>= 0) ruby depends on activemodel (= 4.0.0.beta) ruby rails (>= 0) ruby depends on activemodel (3.2.12) 我已经更新了ruby和rails。 我重新安装了活动模型并重新启动了我的终端。 我的gems文件非常简单: gem ‘rails’ gem ‘jquery-rails’ gem ‘devise’ gem ‘simple_form’ gem ‘protected_attributes’ group :production do gem ‘pg’ end group :development, :test […]

Michael Hartl的Ruby on Rails教程。 第9.1章中的失败测试

嗨,我正在完成Hartl的Ruby on Rails教程,我在第9.1章中遇到了一个失败的测试。 当我在终端中运行spec时,它返回: sis-macbook-pro:sample_app Lagaspi$ bundle exec rspec spec/ ………………………….F………………………….. Failures: 1) AuthenticationPages signin with valid information Failure/Error: it { should have_link(‘Users’, href: users_path) } expected link “Users” to return something # ./spec/requests/authentication_pages_spec.rb:37:in `block (4 levels) in ‘ Finished in 1.42 seconds 64 examples, 1 failure Failed examples: rspec ./spec/requests/authentication_pages_spec.rb:37 # AuthenticationPages signin with […]

捕获各个表单的错误

我试图捕获单个表单的validation错误,因为我在一个页面上有很多。 我使用object_id为每个表单提供了自己唯一的id { id: object.object_id.to_s } do |f| %> 但如果我这样做以捕获错误,我的所有表单上都会出现相同的错误消息 # errors 我试过了 但我明白了 undefined method `errors’ for 59187740:Fixnum 有没有办法解决这个问题 谢谢 编辑 我刚刚注意到,当页面重新加载时validation失败时,表单ID会发生变化,这就解释了为什么无法找到该对象的原因。 我如何保持表单ID相同?

在rails上的ruby中创建一个列表框

有没有一种简单的方法在Ruby on Rails的列表框中显示值? (比如在ruby中添加html select标签的多个属性)

Omniauth错误:没有路线匹配“/ auth / facebook”

我的应用程序在三周前工作正常,但在重新开始工作后,我在尝试通过Facebook登录后收到此错误: No route matches [GET] “/auth/facebook” 不确定发生了什么。 我查看了Stackoverflow,但发现只有与Devise gem有关的帮助。 注意:我没有使用Devise gem。 我的应用程序是在Ruby 2.0和Rails 4上 我的gemlock文件显示了omniauth的这些版本: oauth2 (0.8.1) faraday (~> 0.8) httpauth (~> 0.1) jwt (~> 0.1.4) multi_json (~> 1.0) rack (~> 1.2) omniauth (1.1.4) hashie (>= 1.2, 1.0.2) omniauth-oauth2 (1.0.3) oauth2 (~> 0.8.0) omniauth (~> 1.0) 这是我的routes.rb: Mybrary::Application.routes.draw do get “sessions/create” get “sessions/destroy” resources :users […]

Ruby Mechanize屏幕抓取帮助

我试图在一张桌子上刮一排日期。 我想只刮掉今天有第三行的日期。 这是我的机械化代码。 我正在尝试选择colum row witch今天的日期及其及其列: agent.page.search( “// TD”)的地图(:文本)。.MAP(&:条) Output: “11-02-2011”, “1”, “1”, “1”, “1”, “0”, “0,00 DKK”, “0,00”, “0,00 DKK”, “12-02-2011”, “5”, “5”, “1”, “4”, “0”, “0,00 DKK”, “0,00”, “0,00 DKK”, “14-02-2011”, “1”, “3”, “1”, “1”, “0”, “0,00 DKK”, “,00”, “0,00 DKK”, “7”, “9”, “3”, “6”, “0”, “0,00 DKK”, “0,00”, “0,00 DKK “ 我想只刮掉今天的第三行。

$ gem install rails失败。 任何解决方法?

我出于某种原因在OS X Lion上安装Rails时遇到问题… 在RVM下安装…… $ gem install rails ERROR: While executing gem … (Gem::RemoteFetcher::FetchError) bad response Forbidden 403 (http://production.cf.rubygems.org/gems/rdoc-3.12.gem) $ gem environment RubyGems Environment: – RUBYGEMS VERSION: 1.8.12 – RUBY VERSION: 1.9.3 (2011-10-30 patchlevel 0) [x86_64-darwin11.2.0] – INSTALLATION DIRECTORY: /Users/ethan/.rvm/gems/ruby-1.9.3-p0@mtc_apps – RUBY EXECUTABLE: /Users/ethan/.rvm/rubies/ruby-1.9.3-p0/bin/ruby – EXECUTABLE DIRECTORY: /Users/ethan/.rvm/gems/ruby-1.9.3-p0@mtc_apps/bin – RUBYGEMS PLATFORMS: – ruby – x86_64-darwin-11 […]

如何使“创建”方法可以从外部使用

这是我的方法 def add @result @log = Log.new(params[:log]) if @log.save result = “success”; else result = “fail”; end render :json => result end 这是我的routes.rb match “/logs/add/:value/:category/:note/:own” => “logs#add” 当我尝试这个url时: http://localhost:3000/logs/add/338/testcat/testnote/testown 并且它正确返回json并且新项目已添加到数据库中,但所有字段(value,category,note,own)都为null。 请帮忙 :( 我已经解决了上述问题,但是 如果我想通过从1个url发送到我的ruby来创建多个对象,该怎么办? 例如: 本地主机:3000 /日志/添加/ 338 / testcat / testnote / testown 上面的请求只会创建1个日志到我的数据库如果我想使用上面的解决方案创建许多日志,我必须为一个日志发送一个URL。 我希望我的ruby能够从1个url创建多个日志。 我的意思是我可以通过只向服务器发送一个URL来创建许多日志。 什么routing,method,url应该是什么? (我听说过“ url/add/param1&&param2 ”的内容?