Rails 请大家帮我分析一下这段创建数据库代码

xoxoj · 2012年05月03日 · 最后由 xoxoj 回复于 2012年05月03日 · 3552 次阅读
class CreateSimpleCaptchaData < ActiveRecord::Migration
  def self.up
    create_table :simple_captcha_data do |t|
      t.string :key, :limit => 40
      t.string :value, :limit => 6
      t.timestamps
    end

    add_index :simple_captcha_data, :key, :name => "idx_key"
  end

  def self.down
    drop_table :simple_captcha_data
  end
end

以上这段代码是不是通过 Migration 创建了一个名为 simple_captcha_data 的数据库表啊 其中包含两个字段,一个 string 类型的名为 key 长度为 40 的字段,另一个 string 类型的名为 value 长度为 6 的字段?

还有一个名为"idx_key"的表索引呢。

是这个意思么?

应该是这个索引的名字

啊,应该是这个索引的名字?指的是什么啊!

@xoxoj 就是创建表顺便给 key 那个字段加个索引,idx_key 是这个索引的名字,叫什么都可以

给它取的外号,程序员最喜欢起外号了

哈哈哈,谢谢大家了,另外大家有用过这个 simple_captcha 插件吗?我遇到了一些问题,想请教一下呢、

idx_key 只是给索引取一个名字而已,如果不要 :name 参数的话,Rails 会自动用前面的 :simple_captcha_data:key 来组合索引名字 ,并且在前面加上 index_ 也就是:index_simple_captcha_data_key , 此外 MySQL 里面对于索引名称长度是有限制的

@huacnlee 想请教你一下关于 simple_captcha 的问题,为什么我按照官方的方法来操作,结果还是显示不出来呢。我想办法上传一下图片和错误信息吧

Started GET "/reg" for 127.0.0.1 at 2012-05-03 17:48:00 +0800
Processing by RegController#index as HTML
  [1m[36mConfigs Load (0.2ms)[0m  [1mSELECT content FROM `configs` WHERE `configs`.`describe` = 'site_title' LIMIT 1[0m
  Rendered layouts/_header.html.erb (0.5ms)
  Rendered reg/_styleb.html.erb (31.8ms)
  [1m[35mSimpleCaptcha::SimpleCaptchaData Load (0.2ms)[0m  SELECT `simple_captcha_data`.* FROM `simple_captcha_data` WHERE `simple_captcha_data`.`key` = '5c841c772de0825801882ceb1edf32f0a9492b7c' LIMIT 1
  [1m[36m (0.1ms)[0m  [1mBEGIN[0m
  [1m[35mSQL (0.2ms)[0m  INSERT INTO `simple_captcha_data` (`key`, `value`) VALUES ('5c841c772de0825801882ceb1edf32f0a9492b7c', 'SUFOVY')
  [1m[36m (0.6ms)[0m  [1mCOMMIT[0m
  Rendered simple_captcha/_simple_captcha.erb (0.5ms)
  Rendered reg/index.html.erb within layouts/application (69.4ms)
  [1m[35mConfigs Load (0.2ms)[0m  SELECT content FROM `configs` WHERE `configs`.`describe` = 'site_title' LIMIT 1
  Rendered layouts/_header.html.erb (0.0ms)
  Rendered layouts/_footer.html.erb (0.3ms)
Completed 200 OK in 90ms (Views: 82.7ms | ActiveRecord: 7.5ms)


Started GET "/simple_captcha?code=5c841c772de0825801882ceb1edf32f0a9492b7c&time=1336038481" for 127.0.0.1 at 2012-05-03 17:48:01 +0800
  [1m[36mSimpleCaptcha::SimpleCaptchaData Load (0.3ms)[0m  [1mSELECT `simple_captcha_data`.* FROM `simple_captcha_data` WHERE `simple_captcha_data`.`key` = '5c841c772de0825801882ceb1edf32f0a9492b7c' LIMIT 1[0m
  [1m[35mCACHE (0.0ms)[0m  SELECT `simple_captcha_data`.* FROM `simple_captcha_data` WHERE `simple_captcha_data`.`key` = '5c841c772de0825801882ceb1edf32f0a9492b7c' LIMIT 1

StandardError (Error while running convert: sh: convert: command not found
):
  vendor/plugins/simple-captcha/lib/simple_captcha/utils.rb:17:in `run'
  galetahub-simple_captcha (0.1.3) lib/simple_captcha/image.rb:76:in `generate_simple_captcha_image'
  galetahub-simple_captcha (0.1.3) lib/simple_captcha/middleware.rb:36:in `make_image'
  galetahub-simple_captcha (0.1.3) lib/simple_captcha/middleware.rb:18:in `call'
  actionpack (3.2.3) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
  rack (1.4.1) lib/rack/etag.rb:23:in `call'
  rack (1.4.1) lib/rack/conditionalget.rb:25:in `call'
  actionpack (3.2.3) lib/action_dispatch/middleware/head.rb:14:in `call'
  actionpack (3.2.3) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
  actionpack (3.2.3) lib/action_dispatch/middleware/flash.rb:242:in `call'
  rack (1.4.1) lib/rack/session/abstract/id.rb:205:in `context'
  rack (1.4.1) lib/rack/session/abstract/id.rb:200:in `call'
  actionpack (3.2.3) lib/action_dispatch/middleware/cookies.rb:338:in `call'
  activerecord (3.2.3) lib/active_record/query_cache.rb:64:in `call'
  activerecord (3.2.3) lib/active_record/connection_adapters/abstract/connection_pool.rb:467:in `call'
  actionpack (3.2.3) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
  activesupport (3.2.3) lib/active_support/callbacks.rb:405:in `_run__212775280641935933__call__3618682115726309637__callbacks'
  activesupport (3.2.3) lib/active_support/callbacks.rb:405:in `__run_callback'
  activesupport (3.2.3) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
  activesupport (3.2.3) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (3.2.3) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (3.2.3) lib/action_dispatch/middleware/reloader.rb:65:in `call'
  actionpack (3.2.3) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
  actionpack (3.2.3) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
  actionpack (3.2.3) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  railties (3.2.3) lib/rails/rack/logger.rb:26:in `call_app'
  railties (3.2.3) lib/rails/rack/logger.rb:16:in `call'
  actionpack (3.2.3) lib/action_dispatch/middleware/request_id.rb:22:in `call'
  rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
  rack (1.4.1) lib/rack/runtime.rb:17:in `call'
  activesupport (3.2.3) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.4.1) lib/rack/lock.rb:15:in `call'
  actionpack (3.2.3) lib/action_dispatch/middleware/static.rb:62:in `call'
  railties (3.2.3) lib/rails/engine.rb:479:in `call'
  railties (3.2.3) lib/rails/application.rb:220:in `call'
  rack (1.4.1) lib/rack/content_length.rb:14:in `call'
  railties (3.2.3) lib/rails/rack/log_tailer.rb:14:in `call'
  thin (1.3.1) lib/thin/connection.rb:80:in `block in pre_process'
  thin (1.3.1) lib/thin/connection.rb:78:in `catch'
  thin (1.3.1) lib/thin/connection.rb:78:in `pre_process'
  thin (1.3.1) lib/thin/connection.rb:53:in `process'
  thin (1.3.1) lib/thin/connection.rb:38:in `receive_data'
  eventmachine (0.12.10) lib/eventmachine.rb:256:in `run_machine'
  eventmachine (0.12.10) lib/eventmachine.rb:256:in `run'
  thin (1.3.1) lib/thin/backends/base.rb:61:in `start'
  thin (1.3.1) lib/thin/server.rb:159:in `start'
  rack (1.4.1) lib/rack/handler/thin.rb:13:in `run'
  rack (1.4.1) lib/rack/server.rb:265:in `start'
  railties (3.2.3) lib/rails/commands/server.rb:70:in `start'
  railties (3.2.3) lib/rails/commands.rb:55:in `block in <top (required)>'
  railties (3.2.3) lib/rails/commands.rb:50:in `tap'
  railties (3.2.3) lib/rails/commands.rb:50:in `<top (required)>'
  script/rails:6:in `require'
  script/rails:6:in `<top (required)>'
  -e:1:in `load'
  -e:1:in `<main>'


  Rendered /Users/JasonJiang/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.2.3/lib/action_dispatch/middleware/templates/rescues/_trace.erb (2.3ms)
  Rendered /Users/JasonJiang/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.2.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.1ms)
  Rendered /Users/JasonJiang/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.2.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (9.9ms)


我路由里是这样配置的

match ":controller(/:action)", :controller => /simple_captcha\/[^\/]+/

但是我原本使用的是 restful 路由呢,是不是我路由配错了啊?

没有找到 convert 这个 command,日志中已经非常明确的告诉你原因了。

这个命令来自 imagemagick,这个plugin在 githhub 的项目页的 Requirements 里面清晰的写着:ImageMagick should be installed on your machine to use this plugin. visit www.imagemagick.org/script/index.php for more details.

@lgn21st 可是我已经安装了 ImageMagick 了呀

#11 楼 @xoxoj 那么,你有了 convert 这个命令了么?这个命令是否在你的 $PATH 里面呢?是否能被你正在跑 Rails 应用的这个用户访问到呢?你的 Log 不就是证据嘛?

#11 楼 @xoxoj

~ $ which convert
/usr/local/bin/convert

@lgn21st 你看看我的环境噢!

#14 楼 @xoxoj 我只想说,你的 log 中对错误的信息解释的非常清楚:“Check that you have permissions on /user/local/lib/pkgconfig”

@lgn21st 权限已经有了,symlinked 也已经做好了,可是那个错误仍旧还在 :(

#16 楼 @xoxoj 这里的 log 就是你的最有效的支持信息,你应该坚定的以 log 作为解决问题的切入点。

我无法直接告诉你怎么做,但是我告诉你如果是我,我大概会怎么做:

  • 我首选尝试在 console 中去模拟generate_simple_captcha_image这个行为
  • 如果 console 中无法执行,那么说明 ruby 无法正确的执行你的系统环境命令,这个跟跟系统环境设置有关,朝着这个方向去排查
  • 如果在 console 下能正常执行,那么很可能是 console 载入的环境和 rails server 载入的环境不一致,这个通常发生在用 passenger 来跑的项目中,passenger 并不读取用户的所有系统环境信息,而是有所裁剪,这是一个解决问题的思路。

你的这个问题应该是一个非常简单容易解决的问题,不过依赖你对 *nix 的基本知识的掌握。另外个人给你的建议是读 log 的时候应该非常仔细,在仔细,大部分问题都能通过 log 找到蛛丝马迹,时间长了你会非常喜欢通过 log 排查问题,而不是现在这样自己都不看,直接贴上来让大家帮你看。

@lgn21st 好的,感谢你的热心回复,耽误你很多时间了。我这个是一个测试例子,没有用 passenger,就是直接跑在 thin 下的,我会根据你的提示去努力解决问题的,非常感谢!

需要 登录 后方可回复, 如果你还没有账号请 注册新账号