2

Now i have something like this

http://myapp.com/pages/1
http://myapp.com/pages/2
http://myapp.com/pages/3
http://myapp.com/pages/4

And each page belong to one user

What i need is to each user to set it's own custom name for the page.

I was thinking of using the friendly_id gem http://norman.github.com/friendly_id/ but I don't find any method to directly edit the slug to set a custom friendly url

how should i proceed?

Mr_Nizzle
  • 6,324
  • 11
  • 52
  • 85
  • Please look at this other post: [http://stackoverflow.com/questions/4995759/creating-seo-friendly-urls-in-rails-3][1] Hope that helps, [1]: http://stackoverflow.com/questions/4995759/creating-seo-friendly-urls-in-rails-3 – Ramon Araujo Jan 24 '12 at 07:21

6 Answers6

3

FriendlyID is a great gem.

It shouldn't be hard to implement user defined page URL. Create table pages with user_id and link

class User < ActiveRecord::Base
  has_many :pages

class Page < ActiveRecord::Base
  belongs_to :user

  has_friendly_id :link # link is name of the column whose value will be replaced by slugged value

On the page#new you add an input for the link attribute.

Alternatively, you could set friendly_id on title or something else with :use_slug => true option. This way FriendlyID will take the title and modify it so it doesn't have and restricted characters. It will use it's own table to store slugs. Use cached_slug to increase performanse.

Updated

To give users a choice whether they wan't to set a custom link, you could do this:

  1. Set friendly_id on the link field without slugs..
  2. Make a virtual attribute permalink so you could show it in your forms.
  3. In the before_filter, check whether the permalink is set.
  4. If it is, write it to the link field.
  5. If it's not, write title to the link field.

FriendlyID uses babosa gem to generate slugs. If you decide to use it as well, this is how your filter could look like:

protected
  def generate_link
                #you might need to use .nil? instead
    self.link = self.permalink.empty? ? make_slug(self.title) : make_slug(self.permalink)
  end

  def make_slug(value)
    value.to_slug.normalize.to_s #you could as well use ph6py's way
  end
RAJ
  • 9,353
  • 1
  • 29
  • 61
bassneck
  • 4,006
  • 4
  • 22
  • 31
  • But what if i want to keep it in one field where i have the `title` field for the page.... I may use `has_friendly_id :title` then i can go to to the page using the friendly_id based on the title but i want to give a second option... something like `custom_link` so when the user sets some value there it takes first the `custom_link` instead of the `friendly_id` if the user wants something custom but the title of the page in the `URL` would that be a good way to go? what do you think? – Mr_Nizzle Jul 14 '11 at 19:55
2

Adding to_param method to one of the models should help:

def to_param
  "#{id}-#{call_to_method_that_returns_custom_name.parameterize}"
end

Hope this is what you are looking for :)

user841092
  • 156
  • 3
  • 11
0

I have written a post for this using friendly_id 4.x. It will first create a slug based on the attribute defined, and then allow user to update it using a form. Check it here: http://fedible.org/posts/customizing-friendly_id-slugs .

Saurabh Bhatia
  • 327
  • 3
  • 8
0

There are a variety of ways, you can achieve this-

1) using Stringex

2) sluggable-finder

3) friendly_id

A complete step by step methodology with reasons for each to be used can be found out here. Happy reading!

inquisitive
  • 3,318
  • 4
  • 22
  • 53
0

I am not using the friendly_url gem and am not sure whether my way is efficient. But it works fine for me.

I have a model called Node with id and friendly url field called url_title.

My routes.rb file:

resource 'nodes/:url_title', :to => 'Nodes#view'

nodes_controller.rb

class NodesController <ActiveController
 def view
  @node = Node.find_by_url_title(:params(url_title))
 end
end

And use the @node variable to populate your view.

Now, whenever I type www.example.com/nodes/awesome-title , it takes me to the proper page. One argument against this can be need to create an index on a non-primary field. But I think that might be required for better performance even in the friendly_url gem. Also, the non-primary field url_title needs to be unique. Again, this might be required even for correct working for friendly_url . Feel free to correct me if I am wrong in these assumptions.

brayne
  • 1,323
  • 2
  • 16
  • 26
0

If you watch this screencast, it'll explain it to you. It's old but it's pretty much relevant.

Zach Inglis
  • 1,248
  • 1
  • 13
  • 21