Fliendly_ID - URL 未显示

Fliendly_ID - URL not being displayed

我是 ROR 的新手,正在做一个小项目。最近我添加了友好的 ID gems 并按照文档中的说明进行操作。但我似乎无法让 URL 工作。我将友好 ID 添加到社区模型,这基本上是您的 posts 模型。

路线代码:

class Community < ActiveRecord::Base

extend FriendlyId
friendly_id :title, use: [:slugged, :history]




  validates :title, :length => { :minimum => 5 }

has_attached_file :post_image, :styles => 
           { :medium => "300x300>", :thumb => "100x100>", :large => "1280x720", :headline => "1280x720"}

validates_attachment_content_type :post_image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]

belongs_to :member 
has_many :comments
scope :newest_first, lambda { order("communities.created_at DESC")}
scope :without_community, lambda{|community| community ? {:conditions => ["community.id != ?", @community.id]} : {} }

# def to_param

#   "#{id}-#{title.parameterize}"

# end

end

我什至尝试了 to_param 来获得 URL。但似乎没有任何效果。

控制器代码:

class CommunitiesController < ApplicationController
  before_action :set_community, only: [:show, :edit, :update, :destroy]
  layout "community"
  before_filter :authenticate_member!, except: [:index, :show]
  require 'will_paginate/array'

  # GET /communities
  # GET /communities.json
  def index
    @communities = Community.newest_first.friendly.where(params[:id]).paginate(page: params[:page], per_page: 9)
  end

  # GET /communities/1
  # GET /communities/1.json
  def show
    @communities = Community.newest_first.friendly.where(params[:title]).paginate(page: params[:page], per_page: 5).where('id NOT IN (?)', @community.id)

  end

  # GET /communities/new
  def new
    @community = Community.new({:member_id => current_member.id})
  end

  # GET /communities/1/edit
  def edit
  end

  # POST /communities
  # POST /communities.json
  def create
    @community = Community.new(community_params)

    respond_to do |format|
      if @community.save
        format.html { redirect_to @community, notice: 'Post: #{@community.title.capitalize} was successfully created.' }
        format.json { render :show, status: :created, location: @community }
      else
        format.html { render :new }
        format.json { render json: @community.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /communities/1
  # PATCH/PUT /communities/1.json
  def update
    respond_to do |format|
      if @community.update(community_params)
        format.html { redirect_to @community, notice: 'Post: #{@community.title.capitalize} was successfully updated.' }
        format.json { render :show, status: :ok, location: @community }
      else
        format.html { render :edit }
        format.json { render json: @community.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /communities/1
  # DELETE /communities/1.json
  def destroy
    @community.destroy
    respond_to do |format|
      format.html { redirect_to communities_url, notice: 'Post: #{@community.title.capitalize} was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_community
      @community = Community.friendly.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def community_params
      params.require(:community).permit(:name,:post_image, :title, :content, :member_id)
    end
end

但是,如果我输入 URL,例如 http://localhost:3000/communities/post.title,我可以直接转到 post。这意味着 slug 已经创建并且它们正在工作。但我似乎无法将它们显示在 URL 上。 我尝试了很多排列和组合,但似乎没有任何效果。

非常感谢任何帮助。如果您需要任何其他特定代码,请告诉我。

仅供参考:迁移代码:

class AddSlugToCommunities < ActiveRecord::Migration
  def change
    add_column :communities, :slug, :string, limit: 191
    add_index :communities, :slug, unique: true
  end
end

Friendly_ID 迁移代码:

class CreateFriendlyIdSlugs < ActiveRecord::Migration
  def change
    create_table :friendly_id_slugs do |t|
      t.string   :slug,           :null => false, limit: 191
      t.integer  :sluggable_id,   :null => false
      t.string   :sluggable_type, :limit => 50
      t.string   :scope, limit: 191
      t.datetime :created_at
    end
    add_index :friendly_id_slugs, :sluggable_id
    add_index :friendly_id_slugs, [:slug, :sluggable_type]
    add_index :friendly_id_slugs, [:slug, :sluggable_type, :scope], :unique => true
    add_index :friendly_id_slugs, :sluggable_type
  end
end

深入研究代码,发现正在制作 slug。只是 URL 没有正确链接。对使用 slugs 而不是 :id 的链接进行了一些更改以使其正常工作。

谢谢@uzaif