2

尝试访问我创建的搜索页面时出现错误。问题似乎是我试图在没有空白的情况下删除空白。我的搜索功能有效,但是当 technols 为 nil 时,我无法处理这种情况。

这是我的错误:

NoMethodError in ProjectsController#search

undefined method `[]' for nil:NilClass

在我的项目控制器中,第 26 行:

tech_ids = params["technols"]["id"].reject(&:blank?).

这是我的搜索操作:

def search
  tech_ids = params["technols"]["id"].reject(&:blank?)

  @search = params[:client], params[:industry], params[:role], tech_ids, params[:business_div], params[:project_owner],  params[:status], params[:start_date_dd], params[:start_date_A], params[:start_date_B],  params[:keywords]

  @project_search = Project.search(*@search).order(sort_column + ' ' + sort_direction).paginated_for_index(per_page, page)

  @search_performed = !@search.reject! { |c| c.blank? }.empty? 
  @project = Project.new(params[:project])
  @all_technols = Technol.all
  @project_technol = @project.projecttechnols.build

  respond_to do |format|
    format.html # search.html.erb
    format.json { render :json => @project }
  end
end

这是我的项目模型:

class Project < ActiveRecord::Base
  attr_accessible  :edited_first_name, :edited_last_name, :first_name, :last_name, :business_div, :client, :customer_benifits, :edited_date, :end_date, :entry_date,  :financials, :industry, :keywords, :lessons_learned, :project_name, :project_owner, :role, :start_date, :status, :summary, :tech

  validates_presence_of :business_div, :client, :customer_benifits, :end_date, :exception_pm, :financials, :industry, :keywords, :lessons_learned, :project_name, :project_owner, :role, :start_date, :status, :summary#, :tech

  has_many :projecttechnols
  has_many :technols, :through => :projecttechnols


  def self.like(text); "%#{text}%"; end

  def self.search(search_client, search_industry, search_role, search_techs_ids, search_business_div, search_project_owner, search_status, search_start_date_dd, search_start_date_A, search_start_date_B,  search_keywords)
    # start with a scoped query, to apply more scopes on it afterwards
    _projects = Project.scoped 
    # then, for each of the parameters, apply the scope only if present
    if search_client.present?
      _projects = _projects.where ['client LIKE ?', like(search_client)] 
    end

    if search_industry.present?
      projects = _projects.where ['industry LIKE ?', like(search_industry)]
    end

    if search_role.present?
      _projects = _projects.where ['role LIKE ?', like(search_role)]
    end

    if search_techs_ids.present?
      _projects = _projects.joins(:technols).where("technols.id" => search_techs_ids)
    end

    if search_business_div.present?
      _projects = _projects.where ['business_div LIKE ?', like(search_business_div)]
    end 

    if search_project_owner.present?
      _projects = _projects.where ['project_owner LIKE ?', like(search_project_owner)]
    end

    if search_exception_pm.present?
      _projects = _projects.where ['exception_pm LIKE ?', like(search_exception_pm)]
    end

    if search_status.present?
      _projects = _projects.where ['status LIKE ?', like(search_status)]
    end

    todays_date = DateTime.now.to_date

    if !search_start_date_A.blank? or !search_start_date_B.blank?
      search_start_date_A = Date.parse(search_start_date_A).strftime("%Y-%m-%d")
      search_start_date_B = Date.parse(search_start_date_B).strftime("%Y-%m-%d")
      todays_date = nil
      search_start_date_dd = nil
    end

    if search_start_date_dd.blank?
      todays_date = nil
    end

    if search_start_date_A.present? or search_start_date_B.present?
      _projects = _projects.where [' DATE(start_date) BETWEEN ? AND ?', search_start_date_A, search_start_date_B]
    end

    if search_start_date_dd.present?
      _projects = _projects.where ['DATE(start_date) BETWEEN ? AND ?', search_start_date_dd, todays_date]
    end

    if search_keywords.present?
      _projects = _projects.where ['keywords LIKE ?', like(search_keywords)]
    end

    _projects
  end


  def self.paginated_for_index(projects_per_page, current_page)
    paginate(:per_page => projects_per_page, :page => current_page)
  end
end

我已经尝试在导致问题的行周围包裹一个 if 语句,但似乎仍然无法修复它。

if !params[:technols][:id] = []
tech_ids = params[:technols][:id].reject(&:blank?)
end

我是 Rails 的新手,所以任何解决我的问题的帮助都将不胜感激。提前致谢。

堆栈跟踪:

app/controllers/projects_controller.rb:26:in `search'
actionpack (3.2.6) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.6) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.6) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.6) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.2.6) lib/active_support/callbacks.rb:425:in `_run__464544557__process_action__932090261__callbacks'
activesupport (3.2.6) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.6) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.6) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.6) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.6) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.6) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.6) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.6) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.6) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.6) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.6) lib/action_controller/metal/params_wrapper.rb:206:in `process_action'
activerecord (3.2.6) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.6) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.6) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.6) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.6) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.6) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.6) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.6) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.6) lib/action_dispatch/routing/route_set.rb:36:in `call'
journey (1.0.4) lib/journey/router.rb:68:in `block in call'
journey (1.0.4) lib/journey/router.rb:56:in `each'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.6) lib/action_dispatch/routing/route_set.rb:600:in `call'
warden (1.2.1) lib/warden/manager.rb:35:in `block in call'
warden (1.2.1) lib/warden/manager.rb:34:in `catch'
warden (1.2.1) lib/warden/manager.rb:34:in `call'
actionpack (3.2.6) 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.6) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.6) 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.6) lib/action_dispatch/middleware/cookies.rb:338:in `call'
activerecord (3.2.6) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:473:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.6) lib/active_support/callbacks.rb:405:in `_run__67848467__call__896299115__callbacks'
activesupport (3.2.6) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.6) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.6) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.6) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.6) lib/rails/rack/logger.rb:26:in `call_app'
railties (3.2.6) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.6) 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.6) 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.6) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.6) lib/rails/engine.rb:479:in `call'
railties (3.2.6) lib/rails/application.rb:220:in `call'
rack (1.4.1) lib/rack/content_length.rb:14:in `call'
railties (3.2.6) lib/rails/rack/log_tailer.rb:17:in `call'
thin (1.5.0) lib/thin/connection.rb:81:in `block in pre_process'
thin (1.5.0) lib/thin/connection.rb:79:in `catch'
thin (1.5.0) lib/thin/connection.rb:79:in `pre_process'
thin (1.5.0) lib/thin/connection.rb:54:in `process'
thin (1.5.0) lib/thin/connection.rb:39:in `receive_data'
eventmachine (1.0.0) lib/eventmachine.rb:187:in `run_machine'
eventmachine (1.0.0) lib/eventmachine.rb:187:in `run'
thin (1.5.0) lib/thin/backends/base.rb:63:in `start'
thin (1.5.0) 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.6) lib/rails/commands/server.rb:70:in `start'
railties (3.2.6) lib/rails/commands.rb:55:in `block in <top (required)>'
railties (3.2.6) lib/rails/commands.rb:50:in `tap'
railties (3.2.6) lib/rails/commands.rb:50:in `<top (required)>'
script/rails:6:in `require'
script/rails:6:in `<main>'
4

1 回答 1

11

你的问题是 params[:technols] 是 nil 并且你试图从 nil 对象中检索 id 键的值。你需要改为 -

tech_ids = params[:technols][:id].reject(&:blank?) unless params[:technols].nil?
于 2012-10-03T09:32:30.477 回答