Reputation: 495
I'm a newbie ruby on rails programmer, so forgive me if this is a stupid question... I'm wondering how to parse text after an html form submission. In short, I'm building a twitter rip-off as a personal project. I have an object called 'micropost' (basically a tweet) from which I want to extract hash tags. I have the following regular expression to do the parsing into an array:
micropost.text.gsub(/ (#\w+)/) { |a|
((a.include?('#'))) << a.strip.gsub(/#/, '')
}
however, I'm not quite sure where to place it? Should I put it in the data Model of micropost? In a micropost Helper? In the micropost Controller? Or in the html.erb View for the form.
Thanks so much for any help anyone can offer!
Upvotes: 0
Views: 1191
Reputation: 19193
I'd create a function and put it in a helper since it seems you'll be using it in your views and in your controllers. If you decide to pursue this route and if you want to create a helper for a specific controller and view (microposts_helper, for example) that is not loaded by other controllers and views, you may want to add this line:
config.action_controller.include_all_helpers = false
to your application.rb file located in the config folder.
And finally, since you're a newbie ruby on rails programmer already dealing with regular expressions, I recommend you this site. It's been invaluable to me, as a newbie ruby on rails programmer myself.
Ok, so in your microposts_helper
, create something like:
module MicropostsHelper
def hash_tags(string)
string.gsub(/ (#\w+)/) { |a|
((a.include?('#'))) << a.strip.gsub(/#/, '')
}
end
end
And then you can call it in your microposts views and controller with hash_tags(micropost.text)
Upvotes: 2