1

Help!

It is impossible to realize the right category and subcategory, please help me.

My code

Category.rb

class Category < ActiveRecord::Base

  extend FriendlyId
  friendly_id :title, use: :slugged

  has_many  :scategories, :dependent => :destroy
  validates :title, presence: true

end

scategory.rb

class Scategory < ActiveRecord::Base

  extend FriendlyId
  friendly_id :title, use: :slugged

  belongs_to :category
  has_many   :posts, :dependent => :destroy

end

post.rb

  class Post < ActiveRecord::Base  

    belongs_to    :scategory
    belongs_to    :user

  end

_post.html.haml

.column4.post-block.item
  / Отложенная загрузка картинок "lazy: true" (исправить баг)
  = link_to image_tag(post.post_cover.url(:medium)), post
  .title
    = link_to truncate(post.title, :length => 70), category_scategory_post_url(category_id: @category.slug, scategory_id: @scategory.slug, id: @post.slug)
  -#
    .description
      = truncate(post.description, :length => 70)

routes.rb

  resources :categories do
    resources :scategories do
      resources :posts
    end
  end

Can not find a method of slug, how to properly do?

user3555986
  • 39
  • 1
  • 4

1 Answers1

0

change it to @post = Post.friendly.find_by_slug(params[:slug]).

also change your category and scategory controller like above.

p.s. before these check you've permitted :slug attribute.

p.p.s. also consider this answer for editing routes

Community
  • 1
  • 1
marmeladze
  • 6,468
  • 3
  • 24
  • 45