I can see from here that has_secure_token can generate a random token for a new record in a (User) model. How can I simply generate a random token without needing to do it for a model? (i.e. suppose an app has no model(s) or to put it really simply, to run some (preferably very simple) method, and it should return a random string like pX27zsMN2ViQKta1bGfLmVJE
I have tried gems SecureRandom and digest as recommended in other answers, but neither seems to bundle install successfully (the SO answers were oldish so perhaps those gems are deprecated?) - I'm using ruby 2.5.1 and rails 5.2.3
My goal is simply to generate tokens to use as keys for an API, so I essentially just need randomly generated strings that are safe to use in urls, perhaps a method that I can specify the length of the token (but I'll take what I can get for now!)
Just use SecureRandom. It's not a gem that you have to install. It's built into Ruby:
⇒ ruby -v
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin18]
⇒ irb
2.5.1 :001 > require 'securerandom'
=> true
2.5.1 :002 > SecureRandom.hex(32)
=> "89d45edb28859a905672b707c8f7599f766d12074584ef48a997230dfc0e0998"
2.5.1 :003 > SecureRandom.base64(12)
=> "KaZbhQ7o7U/f9pMs"
2.5.1 :004 > SecureRandom.uuid
=> "ade17ef5-0943-4c70-b417-df7c96c198cd"
Related
I am using rails 4.1.4, ruby 2.1.2 and rvm.
Gemfile (extract)
gem 'rails-perftest'
gem 'ruby-prof', group: :test
I installed ruby using these commands (in order to apply patch which enables memory profiling)
rvm get stable
rvm reinstall 2.1.2 --patch railsexpress
But still no luck and memory reports are empty with rake test:benchmark or rake test:profile
I was trying to get the same Ruby patch and version working with a Rails 3 benchmark and that was broken too, albeit in a different way. It looks to me like this is an oversight in Rails. I'm seeing this warning string on a Rails 3.2 app
$ bundle exec rake test:benchmark
Update your ruby interpreter to be able to run benchmarks.
$ bundle exec rails -v
Rails 3.2.21
The problem seems to be that ActiveSupport 3.2 isn't aware of Ruby versions higher that 2.0 for this particular piece of code
if RUBY_VERSION.between?('1.9.2', '2.0')
require 'active_support/testing/performance/ruby/yarv'
elsif RUBY_VERSION.between?('1.8.6', '1.9')
require 'active_support/testing/performance/ruby/mri'
else
$stderr.puts 'Update your ruby interpreter to be able to run benchmarks.'
exit
end
see https://www.omniref.com/ruby/gems/activesupport/3.2.12/symbols/ActiveSupport::Testing::Performance::Metrics::CpuTime#line=145
After editing the version check manually I can confirm that the patch does work in Rails 3 with version 2.1.2. Perhaps you could check your RUBY_VERSION and RUBY_ENGINE constants for anything unusual?
(I understand this isn't really an answer but I don't have enough reputation to comment. Also it hopefully rules out the rvm patch and ruby-prof as a problem)
I have a mystery to solve when upgrading our Rails3.2 Ruby 1.9 app to a Rails3.2 Ruby 2.1.2 one. Nokogiri seems to break, in that it changes its behavior using open-uri. No gem versions are changed, just the ruby version (this is all on OSX Mavericks, using brew, gcc4 etc).
Steps to reproduce:
$ ruby -v
ruby 1.9.3p484 (2013-11-22 revision 43786) [x86_64-darwin13.1.0]
$ rails console
Connecting to database specified by database.yml
Loading development environment (Rails 3.2.18)
> feed = Nokogiri::XML(open(URI.encode("http://anyblog.wordpress.org/feed/")))
=> #(Document:0x3fcb82f08448 {
name = "document",
children = [
..
> feed.xpath("//item").count
=> 10
So all good! Next, after a rvm change to Ruby 2.1.2 and a bundle install..
$ ruby -v
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0]
$ rails console
Connecting to database specified by database.yml
Loading development environment (Rails 3.2.18)
> feed = Nokogiri::XML(open(URI.encode("http://anyblog.wordpress.org/feed/")))
=>
> feed.inspect
=> "#<Nokogiri::XML::Document:0x86a1f21c name=\"document\">"
> feed.xpath("//item").count
=> 0
So it looks like the 'open' encoding has changed, in that a gzip http stream isn't being fed correctly to nokogiri? I checked with a nokogiri -v and it is using the packaged xml libs rather than system ones. Is this a open-uri Ruby 2.1.2 issue?
Another theory is that one of the gems has monkey patched open-uri to fix something in 1.9 and that is breaking 2.1? Help or ideas please!
EDIT: Here's more info not using Nokogiri, i.e. thinking this is more a open-uri issue on Ruby 2.1.2:
> open(url) {|f|
* f.each_line {|line| p line}
* p f.content_type
* p f.charset
* p f.content_encoding
* }
"\u001F\x8B\b\u0000\u0000\u0000\u0000\u0000\u0000\u0003\xED\x9D\xDBr\eW\xB2\xA6\xAF\xED\xA7\xA8\xCD\u001E\xB7/$\u0010..
(snip)
3\xF3\xA79\xA7\xFAɗ\xFF\u000F\xEAo\x9C\u0014k\xE8\u0000\u0000"
"text/xml"
"utf-8"
["gzip"]
=> ["gzip"]
..the 1.9 version was readable, i.e. gzip was applied already.
If I go into a clean ruby irb it works ok, so it must be something in my rails gems that is changing the behavior of open-uri open to not deflate/gzip. I have a lot of gems referenced.. :(
Ok, here's an answer, and maybe the answer. Ruby 2 changed how it uses headers in HTTP requests and zipping/deflating, but at some point they changed their minds back and put it to be how 1.9 worked. In the interim some Rails gem maintainers monkey patched HTTP:Net to make their gems work on both 1.9 and 2.0. Those monkey patches still linger in older versions of gems and cause issues like I saw upgrading from 1.9 to 2.1
A summary of the issue and solution here:
http://avi.io/blog/2013/12/17/do-not-upgrade-your-rails-project-to-ruby-2-before-you-read-this/
We use the gem right_aws, and the details of that issue with ruby versions is here:
https://github.com/sferik/twitter/issues/473
The solution was to undo the monkey patch using this as a gem reference in our Gemfile:
gem 'right_http_connection', git: 'git://github.com/rightscale/right_http_connection.git', ref: '3359524d81'
Background reading and more info:
https://github.com/rightscale/right_aws/issues/167
rails 3.1 with ruby 1.9.2p290 on windows generates
initialers/config/session_store.rb
Hello::Application.config.session_store :cookie_store, key: '_hello_session'
key: 'hello_session' is :key => 'hello_session' in some of my old apps. What makes rails to generate different codes? Ruby version or rails version ?
There's a new hash syntax in ruby 1.9, you can write:
{key: "hello_session"}
instead of:
{:key => "hello_session"}
The 1.8 syntax is still supported, use it if you want your app to be compatible with both 1.8 and 1.9.
I resolved this by removing my ~/.rvm directory and reinstalling RVM.
I'm not sure exactly what I did but I messed up the Ruby/gem harmony trying to get an older 2.3.5 Rails app working on my system.
I'm currently using Ruby 1.9.2-p0 and rubygems 1.5.7. I've tried the newest one 1.8.2 but it just generates more warnings. I use these alongside of RVM.
I get "can't convert Symbol into Integer" when trying to boot a 2.3.x Rails app no matter what version of rubygems I run. I tried 1.5.7, 1.7.2, and 1.8.2.
$ script/server
=> Booting WEBrick
=> Rails 2.3.5 application starting on http://0.0.0.0:3000
path/.rvm/gems/ruby-1.9.2-p0/gems/rails-2.3.5/lib/rails/gem_dependency.rb:51:in `[]': can't convert Symbol into Integer (TypeError)
Any idea how I can get my system back in harmony?
As Dex and The Tin Man suggested in the comments, the solution was to run rails 2 on ruby 1.8.7 and rails 3 on ruby 1.9.2 using vrm. The warnings generated by rubygems 1.8.2 (now 1.8.3) are solved by running his suggest commands.
I am attempting to use the crypt gem. While I am able to get it installed via bundler, when I do:
require 'crypt/rijndael'
I got an error:
no such file to load -- crypt/rijndael
Any ideas?
Which version of Crypt? Which version of Ruby? Where are you trying to require it from? I tried Crypt 1.1.4 on Ruby 1.8.7 and it worked for me in the rails console:
ruby-1.8.7-p334 :001 > require 'crypt/rijndael'
=> ["Crypt"]
ruby-1.8.7-p334 :002 >
If you're using 1.9, try crypt 1.2.1 instead:
https://github.com/titanous/crypt
Haven't tested it myself, but give it a shot if you're on 1.9