my db schema has a ‘post_text’ and ‘post_html’ column, you can see above how
I am using RedCloth.
But I am wondering since I am new to Ruby, if there is a cleaner way to do
this.
A few comments:
You don’t need the line to set the created_at time - Rails does this
automatically.
You can use: if self.author_name.blank? - nil.blank? is true, and so
is “”.blank? and " ".blank?
Instead of self.post_html.slice(0, regex-match-value), do
self.post_html.slice(0…regex-match-value). This way if the regular
expression is not matched and returns -1, you’ll get the whole content
of the post instead of nil.
Instead of doing teaser = …; return teaser, you can just have the
slice method:
def teaser
self.post_html.slice(0,whatever)
end
Before the form is saved we update the time to be more accurate
def teaser
I am using RedCloth.
But I am wondering since I am new to Ruby, if there is a cleaner way to
do
this.
A few comments:
You don’t need the line to set the created_at time - Rails does this
automatically.
Well, the reason I am using that is if it takes me 10 minutes to write
the
article, I don’t get the time since I clicked “Create New Post” I get
the
time that the post was actually submitted.
Guess that really Isn’t a big deal.
You can use: if self.author_name.blank? - nil.blank? is true, and so
is “”.blank? and " ".blank?
Instead of self.post_html.slice(0, regex-match-value), do
self.post_html.slice(0…regex-match-value). This way if the regular
expression is not matched and returns -1, you’ll get the whole content
of the post instead of nil.
Thanks for the tip! I should hope that it will always be matched, I mean
if
a post is created self.post_html should have at at least one set of
elements.
Instead of doing teaser = …; return teaser, you can just have the
slice method:
def teaser
self.post_html.slice(0,whatever)
end
Yeah the return teaser part comes from my python background
And yes it was a help, Thank you!
Hope this helps,
Dan M.
–
-mike
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.