Rails

Rails: How to to download a file from a http and save it into database

匿名 (未验证) 提交于 2019-12-03 02:18:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: i would like to create a Rails controller that download a serie of jpg files from the web and directly write them into database as binary (I am not trying to do an upload form) Any clue on the way to do that ? Thank you Edit : Here is some code I already wrote using attachment-fu gem : http = Net::HTTP.new('awebsite', 443) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE http.start() { |http| req = Net::HTTP::Get.new("image.jpg") req.basic_auth login, password response = http.request(req) attachment = Attachment.new(:uploaded

How would I save multiple records at once in Rails?

匿名 (未验证) 提交于 2019-12-03 02:18:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: How would I save this array in one call with Rails? tax_rates = [{ :income_from => 0 :income_to => 18200 :start => "01-07-2013" :finish => "30-06-2014" :rate => nil :premium => nil },{ :income_from => 18201 :income_to => 37000 :start => "01-07-2013" :finish => "30-06-2014" :rate => 0.19 :premium => nil },{ :income_from => 18201 :income_to => 37000 :start => "01-07-2013" :finish => "30-06-2014" :rate => 0.19 :premium => nil }] Can I just call Rails.create(tax_rates) ? Also, is there a way to remove duplicate symbols so they look neater? 回答1:

Ruby on Rails: /bin/sh: rspec: command not found

匿名 (未验证) 提交于 2019-12-03 02:16:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm currently going through Michael Hartl's RoR tutorial and am stuck on Chapter 3 when trying to run Spork and Guard. When trying to run tests I get: /bin/sh: rspec: command not found Yes, I did look around for an answer but I don't see where the RubyTest.sublime.settings file is so I don't know how to edit it. Can anyone help me out on how to fix my error? this is my Rubytest.sublime.settings file in my user fodler { "erb_verify_command": "bundle exec erb -xT - {file_name} | ruby -c", "ruby_verify_command": "bundle exec ruby -c {file_name}

Rails and ajax file upload - cannot read property 'innerHTML' of null error

匿名 (未验证) 提交于 2019-12-03 02:16:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am developing a Rails app with a module for dynamic - ajax - image upload to gallery. I am doing it basing on this app - multi-file-upload-demo . I am not very keen in Javascript and stuff, so I copy a lot of logic from that implementation. I made up my app following all logic from rounders demo, I have included all gems and javascript libraries, and I get an error: Uncaught TypeError: Cannot read property 'innerHTML' of null in chrome console, which refers to tmpl.js file tmpl.load = function (id) { return document.getElementById(id)

Ruby on Rails: permission denied when using “rails generate controller welcome”

匿名 (未验证) 提交于 2019-12-03 02:16:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I used Ruby on Rails on Red Hat server. When I trying to generate a controller file, I got this error: [ec2-user@ip-172-31-22-128 testApp4]$ rails generate controller welcome /home/ec2-user/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/lib/spring/server.rb:22:in `initialize': Permission denied @ rb_sysopen - /tmp/spring/fea371aaf9d69cfa58bd12f69b3f1bf6.pid (Errno::EACCES) from /home/ec2-user/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/lib/spring/server.rb:22:in `open' from /home/ec2-user/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/lib/spring/server.rb:22:in

set rails console stack backtrace limit permanently

匿名 (未验证) 提交于 2019-12-03 02:15:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: rails console by default boots with context.back_trace_limit=16 , which can be changed to whatever you want simply by typing context.back_trace_limit=n . The problem is you have to type it each time you boot rails c . Where do I change the context.back_trace_limit permanently? Some more reading on rails console configuration appreciated. 回答1: You have to create/edit your ~/.irbrc with the following: IRB.conf[:BACK_TRACE_LIMIT]= 20 To be taken into account: The options must be uppercased This option is changing not only the rails console, but

Rails custom validation based on a regex?

匿名 (未验证) 提交于 2019-12-03 02:15:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I have the following regex that I use in my routes.rb for /type-in-something-here # A-Z, a-z, 0-9, _ in the middle but never starting or ending in a _ # At least 5, no more than 500 characters In the routes this works well as: match ':uuid' => 'room#show' , : constraints => { : uuid => /[ A - Za - z\d ]([- \w ]{, 498 }[ A - Za - z\d ])?/ } I want to have this also as a validation so invalid records aren't created. So I added the following to room.rb: validates_format_of : uuid , : with => /[ A - Za - z\d ]([- \w ]{, 498 }[ A - Za -

Why are Rails model association results not naturally ActiveRecord::Relations?

匿名 (未验证) 提交于 2019-12-03 02:15:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm using Rails 3.2.0 Let's say I have: class Comment < ActiveRecord::Base has_many :articles end c1 = Comment.last then c1.articles.class # => Array c1.articles.where('id NOT IN (999999)').class # => ActiveRecord::Relation Why is the result of an association not a type of ActiveRecord::Relation ? It clearly is / was at some point : c1.articles.to_orig # undefined method `to_orig' for #<ActiveRecord::Relation:0x007fd820cc80a8> c1.articles.class # => Array Certain evaluations act upon an ActiveRecord::Relation object, but inspecting the class

Rails custom validation based on a regex?

匿名 (未验证) 提交于 2019-12-03 02:14:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I have the following regex that I use in my routes.rb for /type-in-something-here # A-Z, a-z, 0-9, _ in the middle but never starting or ending in a _ # At least 5, no more than 500 characters In the routes this works well as: match ':uuid' => 'room#show' , : constraints => { : uuid => /[ A - Za - z\d ]([- \w ]{, 498 }[ A - Za - z\d ])?/ } I want to have this also as a validation so invalid records aren't created. So I added the following to room.rb: validates_format_of : uuid , : with => /[ A - Za - z\d ]([- \w ]{, 498 }[ A - Za -

Why are Rails model association results not naturally ActiveRecord::Relations?

匿名 (未验证) 提交于 2019-12-03 02:14:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm using Rails 3.2.0 Let's say I have: class Comment < ActiveRecord::Base has_many :articles end c1 = Comment.last then c1.articles.class # => Array c1.articles.where('id NOT IN (999999)').class # => ActiveRecord::Relation Why is the result of an association not a type of ActiveRecord::Relation ? It clearly is / was at some point : c1.articles.to_orig # undefined method `to_orig' for #<ActiveRecord::Relation:0x007fd820cc80a8> c1.articles.class # => Array Certain evaluations act upon an ActiveRecord::Relation object, but inspecting the class