1

ショーアクションのルートを整理する方法について混乱しています。現時点では、特定のガイドラインを選択して「表示」すると、URLには次のように表示されます。

/ガイドライン/1

ここで、1はガイドラインIDです。

言いたいのですが

/タイトル

(これは、表示されているガイドラインのタイトルです)。ルートでこれを管理する方法がわかりません。現時点で私のルートは

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

しかし、これは私が述べたようにガイドライン/ 1を示しているだけなので、私は何か間違ったことをしていることに気づきます

私の見解はこれにリンクしています

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

guides_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

このrailscasthttp://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 に答える