Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>FriendlyID is a great gem.</p> <p>It shouldn't be hard to implement user defined page URL. Create table <code>pages</code> with <code>user_id</code> and <code>link</code></p> <pre><code>class User &lt; ActiveRecord::Base has_many :pages class Page &lt; ActiveRecord::Base belongs_to :user has_friendly_id :link # link is name of the column whose value will be replaced by slugged value </code></pre> <p>On the <code>page#new</code> you add an input for the <code>link</code> attribute. </p> <p>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 <code>cached_slug</code> to increase performanse.</p> <p>Updated</p> <p>To give users a choice whether they wan't to set a custom link, you could do this:</p> <ol> <li>Set friendly_id on the <code>link</code> field without slugs..</li> <li>Make a virtual attribute <code>permalink</code> so you could show it in your forms.</li> <li>In the before_filter, check whether the <code>permalink</code> is set.</li> <li>If it is, write it to the <code>link</code> field.</li> <li>If it's not, write title to the <code>link</code> field.</li> </ol> <p>FriendlyID uses <code>babosa</code> gem to generate slugs. If you decide to use it as well, this is how your filter could look like:</p> <pre><code>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 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload