简体   繁体   中英

Ruby on Rails attribute routing using resources

I'm working on a preservation project where I have models for Institutions, Objects, and Files and I'm using resources in my routes file to help manage all of that. The problem I'm having is that resources uses the :id generated by Ruby on Rails for all of its routes and I would like to use a different attribute. This is particularly important for academic preservation and URL consistency. For example, I would like my show URL for an institution to read institutions/:identifier as opposed to institutions/:id , where :identifier is the attribute I have created. The problem is, I haven't been able to find any information on how to do this aside from just matching individual routes to the new ones I want which seems like a cheap hack at best and breaks a lot of my code.

This is my current routes file:

Fluctus::Application.routes.draw do

  #match "institutions/", to: 'institutions#index', via: [:get]
  #match "institutions/", to: 'institutions#create', via: [:post]
  #match "institutions/:identifier", to: 'institutions#show', via: [:get]
  #match "institutions/:identifier", to: 'institutions#update', via: [:patch]
  #match "institutions/:identifier", to: 'institutions#update', via: [:put]
  #match "institutions/:identifier", to: 'institutions#destroy', via: [:delete]
  #match "institutions/:identifier/edit", to: 'institutions#edit', via: [:get]
  #match "institutions/:identifier/events", to: 'events#index', via: [:get]
  #match "institutions/new", to: 'institutions#new', via: [:get]
  #
  #match "objects/institution_identifier", to: 'intellectual_objects#index', via: [:get], as: "intellectual_objects_path"
  #match "objects/institution_identifier", to: 'intellectual_objects#create', via: [:post]

  resources :institutions do
    resources :intellectual_objects, only: [:index, :create], path: 'objects'
    resources :events, only: [:index]
  end

  resources :intellectual_objects, only: [:show, :edit, :update, :destroy], path: 'objects' do
    resources :generic_files, only: :create, path: 'files'
    patch "files/:id", to: 'generic_files#update', constraints: {id: /.*/}, trailing_slash: true, format: 'json'
    resources :events, only: [:create, :index]
  end

  devise_for :users

  resources :users do
    patch 'update_password', on: :collection
    get 'edit_password', on: :member
    patch 'generate_api_key', on: :member
  end

  resources :generic_files, only: [:show, :destroy], path: 'files' do
    resources :events, only: [:create]
  end

  Blacklight.add_routes(self)

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

  authenticated :user do
    root to: "institutions#show", as: 'authenticated_root'
    # Rails 4 users must specify the 'as' option to give it a unique name
    # root :to => "main#dashboard", :as => "authenticated_root"
  end

  root :to => "catalog#index"
end

You can see where I have attempted to match individual routes without much luck. I've also looked into FriendlyID without any luck so far. If anyone has any ideas it would be greatly appreciated. Thank you!

Unless I'm missing something here, you should just be able to stick the string you want to use into the URL where the id whould be. For example, /objects/show/1 would become /objects/show/some_string . Then in your controller you can find the object using the id parameter:

def show
    @object = Object.where(:some_attribute => params[:id]).first
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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