简体   繁体   English

自定义rails参数中未找到路线错误

[英]No Route Found Error in custom rails params

I'm working on an application where I've had to put together some custom rails parameters for the routes and I keep getting no route found errors when I try to access the page associated with the show method. 我正在开发一个应用程序,其中必须将一些自定义的Rails参数用于路线,当尝试访问与show方法关联的页面时,始终no route found错误。 The application is allowing me to reach my edit pages, so I know it's working on some level but I have to have an error I'm not seeing somewhere that's messing with the normal view. 该应用程序允许我进入edit页面,因此我知道它在某种程度上可以正常工作,但是我必须遇到一个错误,即我看不到某个与正常视图相混淆的地方。 The custom parameters rely on an :identifier that has been custom created for each object. 自定义参数依赖于已为每个对象自定义创建的:identifier Because the application manages several institutions, all with their objects and files, I've had to right several different sets of routes to handle each different thing. 因为该应用程序管理着几个机构,并且全部带有对象和文件,所以我不得不纠正几种不同的路由集来处理每个不同的事情。 The routes for institutions seem to be working fine, but the second set, for :intellectual_objects are the ones that aren't working. 机构的路由似乎运行良好,但是第二种方法是:intellectual_objects是行不通的。

This is my routes file (irrelevant parts excluded): 这是我的路线文件(不包括不相关的部分):

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

This is my IntellectualObject Controller: 这是我的IntelligentObject 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

I'm getting the following error when I try to access the show route (for example: localhost:3000/objects/test.org/126939282): 尝试访问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"]}}

The parameters are showing: 参数显示:

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

And I'm getting this error when I run my spec tests on the IntellectualObjectController 当我跑在我的规格测试,我得到这个错误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"}

I just don't understand because the routes are there, and some of them appear to be working in the application, but every single one is failing in my spec tests. 我只是不明白,因为那里有路由,并且其中一些似乎在应用程序中正常工作,但是我的规格测试中每一个都失败了。 Any and all help is appreciated. 任何和所有帮助表示赞赏。 Thank you. 谢谢。

Your route to intellectual_objects#index has the constraint that the :institution_identifier should match /[\\w+\\.]+/ , but columbia.edu/798d6e812041532c does not match that regexp. 您通往/[\\w+\\.]+/ intellectual_objects#index路线具有以下约束:institution_identifier应该匹配/[\\w+\\.]+/ ,但columbia.edu/798d6e812041532c与该正则表达式不匹配。 Even when you add \\/ to your regexp, I am pretty sure that the slash will confuse Rails routing system. 即使将\\/添加到正则表达式中,我也很确定斜杠会混淆Rails路由系统。

Perhaps you want to change the route to something like this 也许您想将路线更改为类似的内容

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

And than provide columbia.edu (institution_identifier) and 798d6e812041532c (some_id) as separate values. 然后提供columbia.edu (institution_identifier)和798d6e812041532c (some_id)作为单独的值。

According to your error: 根据您的错误:

No route matches {:action=>"index"

It seems you're trying to access the index action? 似乎您正在尝试访问index操作?

Being honest, I couldn't bring myself to look through all your routes (you may want to cut out the irrelevant stuff?) 老实说,我无法让自己审视所有路线(您可能想删掉无关紧要的内容?)

How are you calling the routes with link_to ? 您如何通过link_to调用路线?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM