0

I have installed and used friendly_id to remove the blog id out of my url, and replaced it with the blog title. However, I also need to remove the controller name out of the url:

currently: appname/blogs/one-of-the-blog-posts

what I would like: appname/one-of-the-blog-posts

I'm doing this because I'm rebuilding a site for someone and their twitter posts link to there current site that I'm replacing, but the links of those posts are formatted as such: appname/one-of-the-blog-posts. Since these posts cant be updated via twitter this is the only way I can think of to make them link to the new site I'm building. Any help or suggestions would be much appreciated. I have scoured google for a while and haven't been able to find a definitive answer to this. Thanks in advance.

routes

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

blogs controller

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
halfacreyum
  • 297
  • 5
  • 16

1 Answers1

0

I think you should be able to do:

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

You would need to be careful with managing your routes if you follow this approach, though.

oreoluwa
  • 5,553
  • 2
  • 20
  • 27