如何使用friendly_id从url中删除控制器名称

时间:2016-07-21 21:47:45

标签: ruby-on-rails ruby-on-rails-4 url-routing friendly-id

我已经安装并使用了friendly_id来删除我的网址中的博客ID,并将其替换为博客标题。但是,我还需要从url中删除控制器名称:

目前:appname / blogs / one-of-the-blog-posts

我想要什么:appname / one-of-the-blog-posts

我这样做是因为我正在为某人重建网站,他们的Twitter帖子链接到我正在替换的当前网站,但这些帖子的链接格式如下:appname / one-of-the -blog-职位。由于这些帖子不能通过Twitter更新,这是我能想到的唯一方法,可以将它们链接到我正在构建的新站点。任何帮助或建议将不胜感激。我已经搜索了谷歌一段时间,但未能找到明确的答案。提前谢谢。

路由

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

1 个答案:

答案 0 :(得分:0)

我认为你应该可以做到:

decltype(l)&&

如果你采用这种方法,你需要小心管理你的路线。