My app has a model called items, and items has a row called title. I use
the titles in the url, but also other places in my views, in the page
title, etc.
Since I use the titles in the url, I have a before_save method that
strips out any unfriendly characters and converts spaces to underscores,
etc.
I made a helper (which I called gstring) that does a gsub to convert the
underscores back to spaces, but it seems like I have to type
gstring(item.title) a lot!
I tried to add a method to my item model called "title" that does the
gsub to convert the underscores to spaces, but that gives me a "Stack
level too deep" error. Like this:
def title
  self.title = self.title.gsub("_", " ")
end
It works if I instead call it something like title_with_spaces but I am
trying to type less and not have to think about it.
Another solution that would make some sense to me would be if I could
add my helper as a string modifier so I could use item.title.pretty but
I am not sure how to add a helper or method that works that way. If I do
that I would like .pretty to be available across all models in all of my
views.
I know if I get this working, that I would then have to treat the titles
differently (re-convert to use the underscores) when I use them in a
link_to, but I am going to use a helper for the link_tos anyways, so
that isn''t a big deal.
I am trying to avoid changing everything around so that I store them in
the db with the spaces, then only add the underscores when I use them in
links, as I already have a lot of them in the db with underscores, and
it affects more of my rows than just titles, I am just using that as an
example here.
-- 
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
I had the same problem, this helped me:
write_attribute("title", title.gsub(...))
Have luck!:)
On 25 Lip, 18:59, "Chris G."
<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:> My app has a model called items, and items has a row called title. I use
> the titles in the url, but also other places in my views, in the page
> title, etc.
>
> Since I use the titles in the url, I have a before_save method that
> strips out any unfriendly characters and converts spaces to underscores,
> etc.
>
> I made a helper (which I called gstring) that does a gsub to convert the
> underscores back to spaces, but it seems like I have to type
> gstring(item.title) a lot!
>
> I tried to add a method to my item model called "title" that does
the
> gsub to convert the underscores to spaces, but that gives me a "Stack
> level too deep" error. Like this:
>
> def title
>   self.title = self.title.gsub("_", " ")
> end
>
> It works if I instead call it something like title_with_spaces but I am
> trying to type less and not have to think about it.
>
> Another solution that would make some sense to me would be if I could
> add my helper as a string modifier so I could use item.title.pretty but
> I am not sure how to add a helper or method that works that way. If I do
> that I would like .pretty to be available across all models in all of my
> views.
>
> I know if I get this working, that I would then have to treat the titles
> differently (re-convert to use the underscores) when I use them in a
> link_to, but I am going to use a helper for the link_tos anyways, so
> that isn''t a big deal.
>
> I am trying to avoid changing everything around so that I store them in
> the db with the spaces, then only add the underscores when I use them in
> links, as I already have a lot of them in the db with underscores, and
> it affects more of my rows than just titles, I am just using that as an
> example here.
> --
> Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Paweł K wrote:> I had the same problem, this helped me: > > write_attribute("title", title.gsub(...)) > > Have luck!:)thanks, but where does that go? in my items model? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Yup, in the model :)
So you could make this:
def title=(str)
   write_attribute("title", str.gsub("_", " "))
end
this so far helps with stack problem :)
On 25 Lip, 19:02, "Chris G."
<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:> Paweł K wrote:
> > I had the same problem, this helped me:
>
> > write_attribute("title", title.gsub(...))
>
> > Have luck!:)
>
> thanks, but where does that go? in my items model?
>
> --
> Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---