Ruby on Rails
Saturday, January 26, 2019
On Saturday, January 26, 2019 at 3:04:14 PM UTC-5, Walter Lee Davis wrote:
> On Jan 26, 2019, at 1:57 PM, fugee ohu <fuge...@gmail.com> wrote:
>
>
>
> On Saturday, January 26, 2019 at 12:56:04 PM UTC-5, Walter Lee Davis wrote:
>
> > On Jan 26, 2019, at 5:27 AM, fugee ohu <fuge...@gmail.com> wrote:
> >
> >
> >
> > On Friday, January 25, 2019 at 9:06:22 AM UTC-5, Walter Lee Davis wrote:
> >
> > > On Jan 25, 2019, at 6:00 AM, fugee ohu <fuge...@gmail.com> wrote:
> > >
> > > I wanna convert my news stories to use slug urls based on the :headline column instead of id's but some of the rows contain special characters like double quotes, exclamation points, do I need to gsub those out
> >
> > An interesting theory, but FriendlyId already does this, quite elaborately. The logic that does the "dumbification" is delegated to ActiveSupport's parameterize method, which takes everything not-ASCII and transliterates it into ASCII, and replaces any run of whitespace with a single dash, and removes punctuation. Next, the slug is checked for duplicates with any existing record, and a UUID is appended if so. It's really quite well done and very settled code. I have been using FriendlyId for 8 years or more without encountering any error that wasn't my own mistake.
> >
> > In another of your many threads, you mention that you are trying to create a slug out of the article's text, rather than the headline (as the documentation encourages). You may want to try starting from a truncated copy of the article text (or even following the directions) before you assume that FriendlyId is at fault here. I doubt that your article text will fit into the size constraints of a URL segment, particularly if you worry about IE users seeing the thing at all*'
> >
> > Walter
> >
> > *The entire URL, including prefix, host, port, and path, must be no more than 1,024 characters ASCII in order to be visited by IE.
> >
> >
> > So I have to change the length of the varchar field and the length of the index as well?
>
> I don't know what your schema looks like right now. Here's how I have always used FriendlyID:
>
> 1. I have a `title` column, normal string, 255 characters limit. The client or user can enter whatever she wants up to that limit.
> 2. I use the FriendlyID migration generator to create the slug column on my model.
> 3. I configure FriendlyID according to the instructions (extend, then friendly_id :title, use: :slugged in the model).
> 4. Profit.
>
> There's really nothing else needed except changing find to friendly.find in the controller.
>
> I don't give the client/user the ability to create a too-long slug, by starting from a source text that is short enough.
>
> If you wanted to start from the article text (say, a true text field of thousands of characters), I would start by trimming that text down to the first couple hundred characters. And then, because slugs must be unique, I would imagine you would have quite a few natural collisions, and might end up with a fair amount of disambiguated slugs that end in UUID-shaped garbage strings.
>
> I believe that FriendlyId has the ability to call methods (rather than being bound to just the actual database columns) when calculating the slug, so you could start from `article_body` (a text column, tens of thousands of characters or a clob) and then limit that with truncate or another string-splitting method, like this:
>
> (in your model)
>
> friendly_id :article_preview, use: :slugged
>
> def article_preview
> # cut off at 200 characters, without an ellipsis
> article_text.truncate(200, '')
> end
>
> Does that make sense?
>
> Walter
>
> >
> > --
> > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> > To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com .
> > To post to this group, send email to rubyonra...@googlegroups.com.
> > To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/ .0d80957a-d522-4f30-9aeb- a355ea132b0b%40googlegroups. com
> > For more options, visit https://groups.google.com/d/optout .
>
>
> Yes I forgot that my :headline column is actually text so this would be necessary Also I don't see reason to use friendly_id for anything but the show action, why try to find records by slug for edits, updates and deletes?
>
No, there's no actual reason you have to do this for all routes. You can mix the two (Article.find and Article.friendly.find) within the same controller.
Another thing you may have missed in the documentation is that friendly.find will also find the actual record by its ID, too. So there's no harm in passing the ID in most of the routes and the slug in one route, one method will find either way. I suppose it may be slower that way, but only just a little. This happens here:
https://github.com/norman/friendly_id/blob/master/lib/ friendly_id/finder_methods.rb# L20
Walter
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com .
> To post to this group, send email to rubyonra...@googlegroups.com .
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/ .bb0d026c-f9ec-4286-8329- 3f30a2d99b2a%40googlegroups. com
> For more options, visit https://groups.google.com/d/optout .
TypeError: no implicit conversion of Symbol into Integer
from app/models/press_release.rb:17:in `truncated_headline'
class PressRelease < ApplicationRecord
extend FriendlyId
friendly_id :truncated_headline, use: :slugged
def truncated_headline
# cut off at 200 characters, without an ellipsis
headline.truncate(255, '')
end
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f44d1478-7428-4098-a280-a1dfd056b398%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment