1

我对如何为我的表演活动整理路线感到困惑。目前,如果我选择特定的指南来“显示”它,网址会说

/准则/1

其中 1 是指南 ID

我想说

/标题

(这是正在显示的指南的标题)。我很困惑如何在我的路线中管理这个。目前我的路线有

get 'guideline/:id', to: 'guidelines#show', as: :seeguideline

但这只是我提到的guideline/1,所以我意识到我做错了什么

我的观点与此链接

<%= link_to guideline.title, seeguideline_path(id: guideline.id) %>

在guidelines_controller.rb 中显示操作是

def show
    @guideline = Guideline.where(title: params[:title]).first
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @guideline }

    end
  end

路线是

ActiveAdmin.routes(self)

  devise_for :admin_user, ActiveAdmin::Devise.config


  get "guidelines/topic"
  get "guidelines/topichospital"
  get "guidelines/topicspecialty"
  get "guidelines/favourite"


  devise_for :users

  devise_scope :user do
    get 'signup', to: 'devise/registrations#new', as: :register
    get 'login', to: 'devise/sessions#new', as: :login
    get 'logout', to: 'devise/sessions#destroy', as: :logout
    get 'edit', to: 'devise/registrations#edit', as: :edit
    put 'users' => 'devise/registrations#update', :as => 'user_registration'
    get 'about', to: 'about#about', as: :about
  end

  resources :guidelines
  get 'guidelines', to: 'guidelines#index', as: :guidelines
  get 'favourites', to: "favourites#show", as: :favourites
  get 'topics', to: 'guidelines#list', as: :topics
  get 'hospitals', to: 'guidelines#listhospital', as: :hospitals
  get 'specialties', to: 'guidelines#listspecialty', as: :specialties



  root :to => 'guidelines#index'
  get '/:id', to: 'profiles#show', as: :myprofile
  get '/:title', to: 'guidelines#show', as: :seeguideline
4

2 回答 2

2

你可能想看看这个 railscast http://railscasts.com/episodes/314-pretty-urls-with-friendlyid

于 2013-02-21T08:30:18.047 回答
1

如果你想匹配像'/url'这样的url,你需要把它放在你的路由文件的底部,这样它就优先级最低(即,如果你有一个项目控制器,它不匹配'/projects')。理论上这是通过

match '/:title' => 'guidelines#show', as: :seeguideline

然后在你的控制器中

def show
  @guideline = Guideline.where(title: params[:title]).first
end

那么在你看来,你可以使用

seeguideline_path(@guideline.title)

但您还必须注意要在 url 中使用的标题中的无效字符。

于 2013-02-21T08:29:48.240 回答