I am new to ROR and working on a small project. Recently I added the friendly ID gems and followed the instruction on the documentation. But I can't seem to get the URL working. I added the friendly id to communities model which is basically your posts model.
Route code:
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
I even tried the to_param to get the URL's. But nothing seems to work.
Controller code:
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
But If I type the URL like http://localhost:3000/communities/post.title I am able to go directly to the post. That means the slugs have been created and they are working kind of. But I can't seem to get them displayed on the URL. I have tried many permutation and combinations but nothing seems to work.
Would really appreciate any help. Let me know if there is any other specific code you require.
FYI: Migrations code:
class AddSlugToCommunities < ActiveRecord::Migration
def change
add_column :communities, :slug, :string, limit: 191
add_index :communities, :slug, unique: true
end
end
Friendly_ID migration code:
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
Went deeper into the code and found that slugs were being made. its just that the URL was not being linked properly. Made a few changes to the links to to use slugs instead of :id to get it working.
Thanks anyways @uzaif