My task is to be able to see the path of the current category or product in browsing bar.
At this moment I just can see current category like this
localhost:3000/categories/smalcinataji
but I want like this
localhost:3000/categories/atkritumu-parstrades-tehnika/smalcinataji
To create pretty urls I am using gem called FriendlyId from this example http://railscasts.com/episodes/314-pretty-urls-with-friendlyid?view=asciicast
Thanks!
FriendlyId can take a method to construct the slug.
class Person < ActiveRecord::Base
friendly_id :category_and_subcategory
def category_and_subcategory
"#{my_category_method}/#{my_subcategory_method}"
end
end
Note that there might be an issue with routing due to the additional slash, but there's certainly a fix for this, too, if nescessary.
Related
I'm new to rails and currently involved in an internship and I was assigned to use the friendly_id gem for my tournament class, this is part of the code in it:
class Tournament < ApplicationRecord
extend FriendlyId
friendly_id :url_id
...
end
I don't use a slug since I have a url_id attribute that stores my desired url and when I try with the old .../tournaments/1 everything's all good but with .../tournaments/example I get "example is not a valid value for id" with code 103, status 400. Any ideas what the problem might be?
You have to update your controller for Tournaments so that it uses friendly.find method instead of the find.
# Change this:
Tournament.find(params[:id])
# to
Tournament.friendly.find(params[:id])
I feel this is should be easy but I'm stuck. What's the best way to map a route and have it point to specific url?
User types/click http://acne.com/category-name and it will get http://acne.com/?category[]=1
I don't want to redirect to ?category[]=1 though. I want to make it SEO friendly.
Use the friendly_id gem.
Here is an example o how it could work:
class Category < ActiveRecord::Base
extend FriendlyId
friendly_id :name
end
#category = Category.friendly.find "category-name"
#category.to_param
redirect_to #category # the URL will be /category/category-name
If I understood your question correctly, you want to show the category when user types in acne.com/category-name.
Add this in your config/routes.rb:
get '/:category-name', to: 'category#show'
This will route the request to your categoryController's show method with the user-typed category name in the "category-name" parameter. You can handle what is displayed there (use params[:category-name] to find out the category name the user has typed in)
Sorry for the lack of code tags, I'm on mobile.
I'm current using acts_as_paranoid and friendly_id (5.0.1) on a model and when I destroy a model and try to create a new one that will generate the same slug I get:
ERROR: duplicate key value violates unique constraint "index_papers_on_slug"
I need to somehow get the code that checks if a slug already exists check within the scope of all of the objects not just the non-deleted ones.
How can I get friendly_id to use with_deleted when checking if a slug already exists. I should note that I am also using slug history which may be complicating things further.
Upon digging deeper I realized that since I am using history the slug is being fully deleted while the object is just being soft deleted:
DELETE FROM "friendly_id_slugs" WHERE "friendly_id_slugs"."id" = $1 [["id", 9423]]
So, I just need to figure out how to prevent this and I should be okay since it looks like the friendly_id code itself is already using unscoped when trying to find a valid slug.
Adding the following to the model allowed me to overrride the dependent destroy on the slugs
def has_many_dependent_for_slugs; end
The solution comes from a comment on this github issue.
Friendly_id has a module called scoped which allows you to generate unique slugs within a scope. So, probably
class Paper < ActiveRecord::Base
extend FriendlyId
friendly_id :title, :use => :scoped, :scope => :unscoped
end
will resolve the problem.
I just came across this issue too and I figured two different ways to address it.
Solution 1:
Use dependent: false.
friendly_id :title, dependent: false
Solution: 2
Overcoming this problem without overriding the dependent destroy for anyone that wants to avoid that.
The friendly_id gem uses a method called scope_for_slug_generator to set the model scope. That means we could override this method by adding the following to app/config/initializers/friendly_id.rb.
module FriendlyId
def scope_for_slug_generator
scope = if self.class.base_class.include?(Paranoia)
self.class.base_class.unscoped.with_deleted
else
self.class.base_class.unscoped
end
scope = self.class.base_class.unscoped
scope = scope.friendly unless scope.respond_to?(:exists_by_friendly_id?)
primary_key_name = self.class.primary_key
scope.where(self.class.base_class.arel_table[primary_key_name].not_eq(send(primary_key_name)))
end
end
I want to be able in browsing bar see full path to some product. The path would look like this
www.mysite.com/categories/category_name/subcategory_name/product_name
At this moment I have just
www.mysite.com/categories/category_name
It just provides with one level path, that I don't need.
To build these friendly links I used friendly_id. And full categorization function I created using gem called Ancestry.
How can I modify friendly_id slug so that I can show the full path? I know how the friendly_id works, but I don't know how to change the way how slug is generated.
Some guys could just give me link or tip to search for. I can work it out, I just need idea.
I would be very greatfull :) :)
You can pass a method for the gem to use, as in:
extend FriendlyId
friendly_id :method_name, use: :slugged
def method_name
end
You're probably looking for nested resouces.
If both models (category, subcategory) have slugs, nesting them in your routes.rb file should do the trick:
resources :categories do
resources :subcategories
end
Is it possible to append text to a route's URL, so instead of, say,
http://site/page/2
it comes out
http://site/page/2-cool-stuff-here
?
This should help you out: http://railscasts.com/episodes/314-pretty-urls-with-friendlyid
Since you appear to be okay with keeping the object's id in the url, you can override to_param like so:
class Article < ActiveRecord::Base
def to_param
"#{id} #{name}".parameterize
end
end
which would return .../articles/1-[article name] (As pointed out by the FriendlyId RailsCast mentioned by #Benjamin Tan).
If you desire more functionality than just that, you should check out the FriendlyId gem (see the RailsCast link above).