在RangeError [Refactoring]中为模型结果添加列

时间:2015-01-24 16:09:22

标签: ruby-on-rails ruby sqlite rails-activerecord

我目前正在为铁轨上的红宝石作业做准备。我必须重构一个用Rails编写的博客应用程序并添加合并文章的功能。

我所做的是在编辑文章时显示的表单中添加一个新的文本字段

<%= text_field 'article', 'merge' %>

在我这样做之后,它抱怨文章模型中没有merge方法,所以我继续实施。

我运行以下迁移:

class AddMergeWithToContents < ActiveRecord::Migration
  def self.up
    add_column :contents, :merge_with, :integer
  end

  def self.down
    remove_column :contents, :merge_with
  end
end

然后,当再次访问应用程序时,它会给我以下错误:

 RangeError in Admin::SettingsController#update_database

bignum too big to convert into `long'

Rails.root: /home/saasbook/typo
Application Trace | Framework Trace | Full Trace

app/controllers/admin/settings_controller.rb:38:in `[]'
app/controllers/admin/settings_controller.rb:38:in `update_database'

完整追踪

app/controllers/admin/settings_controller.rb:38:in `[]'
app/controllers/admin/settings_controller.rb:38:in `update_database'
actionpack (3.0.17) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.0.17) lib/abstract_controller/base.rb:150:in `process_action'
actionpack (3.0.17) lib/action_controller/metal/rendering.rb:11:in `process_action'
actionpack (3.0.17) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.0.17) lib/active_support/callbacks.rb:457:in `block in _run__331447277__process_action__533401003__callbacks'
activesupport (3.0.17) lib/active_support/callbacks.rb:221:in `block in _conditional_callback_around_20300'
activesupport (3.0.17) lib/active_support/callbacks.rb:331:in `around'
activesupport (3.0.17) lib/active_support/callbacks.rb:315:in `_callback_around_20297'
activesupport (3.0.17) lib/active_support/callbacks.rb:220:in `_conditional_callback_around_20300'
activesupport (3.0.17) lib/active_support/callbacks.rb:456:in `_run__331447277__process_action__533401003__callbacks'
activesupport (3.0.17) lib/active_support/callbacks.rb:410:in `_run_process_action_callbacks'
activesupport (3.0.17) lib/active_support/callbacks.rb:94:in `run_callbacks'
actionpack (3.0.17) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.0.17) lib/action_controller/metal/rescue.rb:17:in `process_action'
actionpack (3.0.17) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.0.17) lib/active_support/notifications.rb:52:in `block in instrument'
activesupport (3.0.17) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.0.17) lib/active_support/notifications.rb:52:in `instrument'
actionpack (3.0.17) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.0.17) lib/abstract_controller/base.rb:119:in `process'
actionpack (3.0.17) lib/abstract_controller/rendering.rb:41:in `process'
actionpack (3.0.17) lib/action_controller/metal.rb:138:in `dispatch'
actionpack (3.0.17) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.0.17) lib/action_controller/metal.rb:178:in `block in action'
actionpack (3.0.17) lib/action_dispatch/routing/route_set.rb:68:in `call'
actionpack (3.0.17) lib/action_dispatch/routing/route_set.rb:68:in `dispatch'
actionpack (3.0.17) lib/action_dispatch/routing/route_set.rb:33:in `call'
rack-mount (0.6.14) lib/rack/mount/route_set.rb:148:in `block in call'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:93:in `block in recognize'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:75:in `optimized_each'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:92:in `recognize'
rack-mount (0.6.14) lib/rack/mount/route_set.rb:139:in `call'
actionpack (3.0.17) lib/action_dispatch/routing/route_set.rb:499:in `call'
actionpack (3.0.17) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
actionpack (3.0.17) lib/action_dispatch/middleware/head.rb:14:in `call'
rack (1.2.5) lib/rack/methodoverride.rb:24:in `call'
actionpack (3.0.17) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.0.17) lib/action_dispatch/middleware/flash.rb:182:in `call'
actionpack (3.0.17) lib/action_dispatch/middleware/session/abstract_store.rb:149:in `call'
actionpack (3.0.17) lib/action_dispatch/middleware/cookies.rb:302:in `call'
activerecord (3.0.17) lib/active_record/query_cache.rb:32:in `block in call'
activerecord (3.0.17) lib/active_record/connection_adapters/abstract/query_cache.rb:28:in `cache'
activerecord (3.0.17) lib/active_record/query_cache.rb:12:in `cache'
activerecord (3.0.17) lib/active_record/query_cache.rb:31:in `call'
activerecord (3.0.17) lib/active_record/connection_adapters/abstract/connection_pool.rb:354:in `call'
actionpack (3.0.17) lib/action_dispatch/middleware/callbacks.rb:46:in `block in call'
activesupport (3.0.17) lib/active_support/callbacks.rb:416:in `_run_call_callbacks'
actionpack (3.0.17) lib/action_dispatch/middleware/callbacks.rb:44:in `call'
rack (1.2.5) lib/rack/sendfile.rb:106:in `call'
actionpack (3.0.17) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
actionpack (3.0.17) lib/action_dispatch/middleware/show_exceptions.rb:47:in `call'
railties (3.0.17) lib/rails/rack/logger.rb:13:in `call'
rack (1.2.5) lib/rack/runtime.rb:17:in `call'
activesupport (3.0.17) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.2.5) lib/rack/lock.rb:13:in `block in call'
<internal:prelude>:10:in `synchronize'
rack (1.2.5) lib/rack/lock.rb:13:in `call'
actionpack (3.0.17) lib/action_dispatch/middleware/static.rb:30:in `call'
railties (3.0.17) lib/rails/application.rb:168:in `call'
railties (3.0.17) lib/rails/application.rb:77:in `method_missing'
railties (3.0.17) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.2.5) lib/rack/content_length.rb:13:in `call'
rack (1.2.5) lib/rack/handler/webrick.rb:52:in `service'
/home/saasbook/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/home/saasbook/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/home/saasbook/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'

这是设置控制器

class Admin::SettingsController < Admin::BaseController
  cache_sweeper :blog_sweeper

  def index
    if this_blog.base_url.blank?
      this_blog.base_url = blog_base_url
    end
    load_settings
  end

  def write; load_settings end
  def feedback; load_settings end
  def errors; load_settings; end

  def redirect
    flash[:notice] = _("Please review and save the settings before continuing")
    redirect_to :action => "index"
  end

  def update
    if request.post?
      Blog.transaction do
        params[:setting].each { |k,v| this_blog.send("#{k.to_s}=", v) }
        this_blog.save
        flash[:notice] = _('config updated.')
      end

      redirect_to :action => params[:from]
    end
  rescue ActiveRecord::RecordInvalid
    render params[:from]
  end

  def update_database
    @current_version = Migrator.current_schema_version
    @needed_version = Migrator.max_schema_version
    @support = Migrator.db_supports_migrations?
    @needed_migrations = Migrator.available_migrations[@current_version..@needed_version].collect do |mig|
      mig.scan(/\d+\_([\w_]+)\.rb$/).flatten.first.humanize
    end
  end

  def migrate
    if request.post?
      Migrator.migrate
      redirect_to :action => 'update_database'
    end
  end

  private
  def load_settings
    @setting = this_blog
  end
end

任何帮助都会非常感激。感谢。

1 个答案:

答案 0 :(得分:2)

因此,要回答问题,您可以将text_field更改为text_field_tag并删除第一个参数。或者你可以添加一个&#34; setter&#34;您的文章模型名为merge=(val)的方法,您可以在其中编写所需的代码。希望这有帮助!