1

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.

The Gugaru
  • 618
  • 6
  • 23
  • 1
    Read the official documentation and start again on your implementation. You´re making it difficult on yourself by overcomplicating it. FriendlyID does not override the ActiveRecord finders anymore. That behavior was made optional a long time ago and should not be used. – max Nov 22 '16 at 20:31
  • I have read the official documentation, and that is why I am here. I have reimplemented several times and the result is always the same. Either with spaces or dashes with the id in the front. I removed the finders and still the behavior continues. I just finished doing it on an empty project exactly as the documentation prescribes however I am still getting urls with spaces in them. – The Gugaru Nov 22 '16 at 20:48
  • Thats because you are using the name and not the slug column in your `to_param` override. – max Nov 23 '16 at 01:23

0 Answers0