0

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!

2
  • I tried to format your code, but couldn't tell if it was entered correctly in the first place due to smart-quotes and what seems like a surplus of parens. Please double-check, and cut-and-paste only pure text. Commented Jan 31, 2012 at 2:23
  • Thanks for the heads up on the extra parenthesis. Sorry about that. Commented Jan 31, 2012 at 20:26

1 Answer 1

2

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)

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry... you mean create a method in the helper that has this regExp as a function, right?
That's what I thought you meant, just had to double check :P Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.