2016-07-21 5 views
0

friendly_idをインストールして使用して、自分のURLからブログIDを削除し、ブログタイトルに置き換えました。しかし、私はまた、URLからコントローラ名を削除する必要があります。friendly_idを使用してURLからコントローラ名を削除する方法

現在:appnameの/ブログ/ワンのブログ記事

を私が望むものを:APPNAME/1ブログの投稿

私は誰かのためにサイトを再構築しているので、私はこれをやっています。彼らのtwitter投稿は、私が置き換えている現在のサイトにリンクしていますが、それらの投稿のリンクはそのような:appname/one-of-the-blog-posts。これらの投稿はツイッターを介して更新できないので、私が構築している新しいサイトにそれらをリンクさせるために考えることができる唯一の方法です。どんな助けや提案も大歓迎です。私はしばらくの間Googleを精査しており、これに対する決定的な答えを見つけることができませんでした。前もって感謝します。

ルート

AppName::Application.routes.draw do 

    root 'pages#index' 
    get 'about', to: 'pages#about' 
    get 'contacts', to: 'contacts#new' 
    resources 'contacts', only: [:new, :create] 
    get 'locations/tampa', to: 'locations#tampa' 
    get 'locations/jacksonville', to: 'locations#jacksonville' 
    get 'locations/orlando', to: 'locations#orlando' 
    get 'locations/st_petersburg', to: 'locations#stpetersburg' 
    resources :locations do 
    resources :photos, only: :create 
    end 


    resources 'blogs' 


    get 'faqs', to: 'pages#faqs' 
    get 'whats_included', to: 'pages#whats_included' 

end 

ブログコントローラ

class BlogsController < ApplicationController 
before_action :set_blog, only: [:edit, :update, :show] 

    def index 
    @blogs = Blog.order("created_at DESC") 
    end 
    def new 
    @blog = Blog.new 
    end 
    def create 
    @blog = Blog.new(blog_params) 
    if @blog.save 
     flash[:notice] = "New Blog Added" 
     redirect_to blogs_path 
    else 
     flash.now[:error] = 'Cannot Create Blog' 
     render 'new' 
    end 
    end 
    def edit 

    end 
    def update 
    @blog.update_attributes(blog_params) 
    redirect_to blogs_path(@blog) 
    end 
    def show 
    @location = Location.all 
    end 

    private 

    def set_blog 
    @blog = Blog.friendly.find(params[:id]) 
    end 
    def blog_params 
    params.require(:blog).permit(:title, :description, :date, :property, :city, :author) 
    end 
end 

答えて

0

私はあなたが行うことができるはずだと思う:

AppName::Application.routes.draw do 

    root 'pages#index' 
    get 'about', to: 'pages#about' 
    get 'contacts', to: 'contacts#new' 
    resources 'contacts', only: [:new, :create] 
    get 'locations/tampa', to: 'locations#tampa' 
    get 'locations/jacksonville', to: 'locations#jacksonville' 
    get 'locations/orlando', to: 'locations#orlando' 
    get 'locations/st_petersburg', to: 'locations#stpetersburg' 
    resources :locations do 
    resources :photos, only: :create 
    end 

    get 'faqs', to: 'pages#faqs' 
    get 'whats_included', to: 'pages#whats_included' 

    resources 'blogs', path: '/' 
end 

あなたはあなたの管理に注意する必要があるだろうあなたがtに従うならばルート彼のアプローチ。

+0

私はこれを試しましたが、残念ながら私はエラーが発生します:ActiveRecord :: RecordNotFound – halfacreyum

関連する問題