customize routes

#指定controller
resources :photos, controller: 'images'
HTTP Verb    Path                Controller#Action    Named Helper
GET          /photos             images#index         photos_path
GET          /photos/new         images#new           new_photo_path
POST         /photos             images#create        photos_path
GET          /photos/:id         images#show          photo_path(:id)
GET          /photos/:id/edit    images#edit          edit_photo_path(:id)
PATCH/PUT    /photos/:id         images#update        photo_path(:id)
DELETE       /photos/:id         images#destroy       photo_path(:id)
#或者
resources :user_permissions, controller: 'admin/user_permissions' #controller: 'Admin::UserPermissions'


#params指定constraints
resources :photos, constraints: { id: /[A-Z][A-Z][0-9]+/ }
#或者
constraints(id: /[A-Z][A-Z][0-9]+/) do
  resources :photos
  resources :accounts
end

#指定named helper
resources :photos, as: 'images'
HTTP Verb    Path                 Controller#Action    Named Helper
GET          /photos              photos#index         images_path
GET          /photos/new          photos#new           new_image_path
POST         /photos              photos#create        images_path
GET          /photos/:id          photos#show          image_path(:id)
GET          /photos/:id/edit     photos#edit          edit_image_path(:id)
PATCH/PUT    /photos/:id          photos#update        image_path(:id)
DELETE       /photos/:id          photos#destroy       image_path(:id)
#nested named helper
resources :magazines do
  resources :ads, as: 'periodical_ads'
end
生成:magazine_periodical_ads_url和edit_magazine_periodical_ad_path

#改变new和edit
resources :photos, path_names: { new: 'make', edit: 'change' }
#或者
scope path_names: { new: 'make' } do
  # rest of your routes
end
/photos/make  #=>"photos#new"
/photos/1/change  #=>"photos/1/show"

#使用prefix的routes helper
scope 'admin' do
  resources :photos, as: 'admin_photos'
end
#或者
scope 'admin', as: 'admin' do
  resources :photos, :accounts
end
admin_photos_path #=>"/admin/photos"

#限制 routes
resources :photos, except: :destroy
resources :photos, only: [:index, :show]

#变化url
scope(path_names: { new: 'neu', edit: 'bearbeiten' }) do
  resources :categories, path: 'kategorien'
end
HTTP Verb    Path                          Controller#Action       Named Helper
GET          /kategorien                   categories#index        categories_path
GET          /kategorien/neu               categories#new          new_category_path
POST         /kategorien                   categories#create       categories_path
GET          /kategorien/:id               categories#show         category_path(:id)
GET          /kategorien/:id/bearbeiten    categories#edit         edit_category_path(:id)
PATCH/PUT    /kategorien/:id               categories#update       category_path(:id)
DELETE       /kategorien/:id               categories#destroy      category_path(:id)
#定制单复数config/initializers/inflections.rb
ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'tooth', 'teeth'
end

#override routes parameter
resources :videos, param: :identifier
class Video < ApplicationRecord
  def to_param
    identifier
  end
end
video = Video.find_by(identifier: "Roman-Holiday")

results matching ""

    No results matching ""