自定义rails params中找不到路由错误

时间:2014-04-04 09:44:14

标签: ruby-on-rails ruby parameters url-routing

我正在开发一个应用程序,我必须为路由组合一些自定义rails参数,当我尝试访问与no route found关联的页面时,我不断收到show错误方法。该应用程序允许我到达我的edit页面,所以我知道它在某种程度上工作,但我必须有一个错误,我没有看到某个地方正在弄乱普通视图。自定义参数依赖于为每个对象自定义创建的:identifier。因为应用程序管理多个机构,所有机构都有自己的对象和文件,所以我必须使用几组不同的路由来处理每个不同的事情。机构的路线似乎工作正常,但第二组,:intellectual_objects是不起作用的。

这是我的路线档案(不相关的部分除外):

Fluctus::Application.routes.draw do

  get "institutions/:institution_identifier/objects", to: 'intellectual_objects#index', as: :institution_intellectual_objects, :constraints => { :institution_identifier => /[\w+\.]+/ }
  post "institutions/:institution_identifier/objects", to: 'intellectual_objects#create', :constraints => { :institution_identifier => /[\w+\.]+/ }

  #Intellectual Object Routes
  #get "objects/:institution_identifier", to: 'intellectual_objects#index', as: :institution_intellectual_objects, :constraints => { :institution_identifier => /[\w+\.]+/ }
  #post "objects/:institution_identifier", to: 'intellectual_objects#create', :constraints => { :institution_identifier => /[\w+\.]+/ }

  patch "objects/:intellectual_object_identifier", to: 'intellectual_objects#update', :constraints => { :intellectual_object_identifier => /[\w+\/\.]+/ }
  put "objects/:intellectual_object_identifier", to: 'intellectual_objects#update', :constraints => { :intellectual_object_identifier => /[\w+\/\.]+/ }
  delete "objects/:intellectual_object_identifier", to: 'intellectual_objects#destroy', :constraints => { :intellectual_object_identifier => /[\w+\/\.]+/ }
  get "objects/:intellectual_object_identifier/edit", to: 'intellectual_objects#edit', as: :edit_intellectual_object, :constraints => { :intellectual_object_identifier => /[\w+\/\.]+/ }
  get "objects/:intellectual_object_identifier/events", to: 'events#index', as: :intellectual_object_events, :constraints => { :intellectual_object_identifier => /[\w+\/\.]+/ }
  post "objects/:intellectual_object_identifier/events", to: 'events#create', :constraints => { :intellectual_object_identifier => /[\w+\/\.]+/ }
  get "objects/:intellectual_object_identifier", to: 'intellectual_objects#show', as: :intellectual_object, :constraints => { :intellectual_object_identifier => /[\w+\/\.]+/ }

  #post "objects/institution_identifier/:intellectual_object_identifier/data", to: 'generic_files#create', as: intellectual_object_generic_files, :constraints => { [:intellectual_object_identifier, :institution_identifier] => /[\w+\.]/ }
  #patch "objects/institution_identifier/:intellectual_object_identifier/data/:filename", to: 'generic_files#update', :constraints => { [:intellectual_object_identifier, :institution_identifier] => /[\w+\.]/ }

  Blacklight.add_routes(self)

  mount Hydra::RoleManagement::Engine => '/'

  root :to => "catalog#index"
end

这是我的IntellectualObject Controller:

class IntellectualObjectsController < ApplicationController
  before_filter :authenticate_user!
  #load_and_authorize_resource :institution, only: [:index, :create]
  load_and_authorize_resource :through => :institution, only: :create
  #load_and_authorize_resource except: [:index, :create]
  before_filter :set_object, only: [:show, :edit, :update, :destroy]
  before_filter :set_institution, only: [:index, :create]

  include Aptrust::GatedSearch
  apply_catalog_search_params
  include RecordsControllerBehavior

  self.solr_search_params_logic += [:for_selected_institution]

  def update
    if params[:counter]
      # They are just updating the search counter
      search_session[:counter] = params[:counter]
      redirect_to :action => "show", :status => 303
    else
      # They are updating a record. Use the method defined in RecordsControllerBehavior
      super
    end
  end

  def destroy
    resource.soft_delete
    respond_to do |format|
      format.json { head :no_content }
      format.html {
        flash[:notice] = "Delete job has been queued for object: #{resource.title}"
        redirect_to root_path
      }
    end
  end

  protected 

  # Override Hydra-editor to redirect to an alternate location after create
  def redirect_after_update
    intellectual_object_path(resource)
  end

  def self.cancan_resource_class
    CanCan::ControllerResource
  end

  private

  def for_selected_institution(solr_parameters, user_parameters)
    #puts "In for_selected_institution------------------------------------------"
    #puts params[:institution_identifier]
    #puts params[:intellectual_object_identifier]
    if(params[:institution_identifier])
      institution = Institution.where(desc_metadata__institution_identifier_tesim: params[:institution_identifier]).first
    else
      io = IntellectualObject.where(desc_metadata__intellectual_object_identifier_tesim: params[:intellectual_object_identifier]).first
      institution = io.institution
    end
    #puts "INSTITUTION: #{institution.id}"
    solr_parameters[:fq] ||= []
    solr_parameters[:fq] << ActiveFedora::SolrService.construct_query_for_rel(is_part_of: "info:fedora/#{institution.id}")
  end

  # Override Blacklight so that it has the "institution_identifier" set even when we're on a show page (e.g. /objects/foo:123)
   def search_action_url options = {}
    institution_intellectual_objects_path(params[:institution_identifier] || @intellectual_object.institution.institution_identifier)
  end

  def set_institution
    if params[:institution_identifier].nil? || Institution.where(desc_metadata__institution_identifier_tesim: params[:institution_identifier]).empty?
      redirect_to root_url
      flash[:alert] = "Sonething wrong with institution_identifier."
    else
      @institution = Institution.where(desc_metadata__institution_identifier_tesim: params[:institution_identifier]).first
      authorize! [:create, :index], @institution if cannot? :read, @institution
    end
  end

  def set_object
    if params[:intellectual_object_identifier].nil? || IntellectualObject.where(desc_metadata__intellectual_object_identifier_tesim: params[:intellectual_object_identifier]).empty?
      redirect_to root_url
      flash[:alert] = "Something wrong with intellectual_object_identifier."
    else
      io_options = IntellectualObject.where(desc_metadata__intellectual_object_identifier_tesim: params[:intellectual_object_identifier])
      io_options.each do |io|
        if params[:intellectual_object_identifier] == io.intellectual_object_identifier
          @intellectual_object = io
          @institution = @intellectual_object.institution
        end
      end
      if @intellectual_object.nil?
        redirect_to root_url
        flash[:alert] = "The object you requested does not exist."
      end
      #authorize! [:show, :edit, :update, :destroy], @institution if cannot? :read, @institution
    end
  end
end

当我尝试访问show路由时,我收到以下错误(例如:localhost:3000 / objects / test.org / 126939282):

ActionController::UrlGenerationError in IntellectualObjects#show
Showing /Users/kec6en/HydraApp/fluctus/app/views/intellectual_objects/_facet_limit.html.erb where line #11 raised:

No route matches {:action=>"index", :intellectual_object_identifier=>"columbia.edu/798d6e812041532c", :controller=>"intellectual_objects", :f=>{"institution_name_ssi"=>["Columbia University"]}}

参数显示:

{"intellectual_object_identifier"=>"columbia.edu/798d6e812041532c"}

当我在IntellectualObjectController

上运行我的规范测试时,我收到此错误
Failure/Error: get :show, intellectual_object_identifier: obj1
ActionController::UrlGenerationError:
   No route matches {:intellectual_object_identifier=>"kaulkedurgan.org13/39b1eb47-da8b-4145-b03b-5f1851407012", :controller=>"intellectual_objects", :action=>"show"}

我只是不明白,因为路由在那里,其中一些似乎在应用程序中工作,但每一个都在我的规范测试中失败。任何和所有的帮助表示赞赏。谢谢。

2 个答案:

答案 0 :(得分:1)

您前往intellectual_objects#index的路线约束:institution_identifier应与/[\w+\.]+/匹配,但columbia.edu/798d6e812041532c与该正则表达不匹配。即使你向正则表达式添加\/,我也很确定斜杠会混淆Rails路由系统。

也许您想要将路线更改为此类

get "institutions/:institution_identifier/:some_id/objects", 
    to: 'intellectual_objects#index', 
    as: :institution_intellectual_objects, 
    constraints: { institution_identifier: /[\w+\.]+/ }

而不是将columbia.edu(institution_identifier)和798d6e812041532c(some_id)作为单独的值提供。

答案 1 :(得分:0)

根据你的错误:

No route matches {:action=>"index"

您似乎正在尝试访问index操作?

说实话,我无法通过你所有的路线(你可能想要删除无关的东西?)

您如何使用link_to来调用路线?