Ok folks I have been struggling with this for a couple days and I am totally confused at this point.
I am running Rails 5 and Ruby 2.3.0 From my understanding on ROR that if I load a url with like this
post/3-DIY-Mirror
ActiveRecord is able to interpret the post id by doing to_i on the string and which would leave just the integer 3 which is the id of the post. Now I understand FriendlyId is suppose to be able to override that behavior and have ActiveRecord find DIY-Mirror without the integer 3 by doing Post.friendly.find(params[:id]) I have followed several sets of instructions and I end up with 2 types of behavior and not the one that I believe FriendlyId is suppose to give me.
CASE 1 In my controller I have:
def show
@page = Page.find(params[:id])
if request.path != page_path(@page)
redirect_to @page, status: :moved_permanently
end
end
in my Model I have:
class Page < ApplicationRecord
extend FriendlyId
friendly_id :name, use: [:slugged, :finders, :history]
validates :name, :slug, presence: true
def to_param
[id, name.parameterize].join("-")
end
def should_generate_new_friendly_id?
send(friendly_id_config.slug_column).nil? &&
!send(friendly_id_config.base).nil?
end
end
This returns a url like http://localhost:3000/pages/4-sdfasdf and it works. However the default ActiveRecord Behavior is there with the id at the beginning of the string.
CASE 2 Now if I take my controller to:
def show
@page = Page.friendly.find(params[:id])
if request.path != page_path(@page)
redirect_to @page, status: :moved_permanently
end
end
and my model to:
class Page < ApplicationRecord
extend FriendlyId
friendly_id :name, use: [:slugged, :finders, :history]
validates :name, :slug, presence: true
end
I get it without the id and it works, however the URL has spaces instead of hyphens?! http://localhost:3000/pages/asdf asdfas asdfas
I have followed several of the guides on github and none of the produce http://localhost:3000/pages/asdf-asdfas-asdfas . They produce either of the above results instead. Anyone have any light they can shine on this as I am sure I have setup everything correctly but it just doesn't seem to be putting the dashes in automatically.