Interacting Between Controllers.

18 views
Skip to first unread message

Alex B.

unread,
Jan 31, 2015, 1:01:18 PM1/31/15
to rubyonra...@googlegroups.com
I am having issue grasping how to accomplish interacting between two
controller.

My design is as follows: I am coding a Microblog. I can make
posts, which are text, and need to extract hashtags from that content.
Right now I use a form_for to get the input for the micropost. I have
put in the models that a micropost has_many Hashtags and that Hashtags
belongs_to micropost.

My question is where do I put the code to do this? I can't seem to make
a new hashtag directly from the view in a similar way as my microposts.
My initial try was to put the extract code in the create method of the
Post controller. Anything pointing me in the right direction would be
great.

--
Posted via http://www.ruby-forum.com/.

Hassan Schroeder

unread,
Jan 31, 2015, 2:22:30 PM1/31/15
to rubyonrails-talk
On Sat, Jan 31, 2015 at 10:00 AM, Alex B. <li...@ruby-forum.com> wrote:
> I am having issue grasping how to accomplish interacting between two
> controller.

You shouldn't be thinking about controllers here at all.

> My design is as follows: I am coding a Microblog. I can make
> posts, which are text, and need to extract hashtags from that content.
> Right now I use a form_for to get the input for the micropost. I have
> put in the models that a micropost has_many Hashtags and that Hashtags
> belongs_to micropost.
>
> My question is where do I put the code to do this?

Since the data that has to be processed is in the Micropost, why not
have the hashtag creation method there?

If you invoke that method on a callback like before_save, then you'll
be able to edit the post and have the hashtags updated each time.

Alternatively you could use a "factory" class to build the hashtags
with the Micropost as the input.

HTH!
--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
http://about.me/hassanschroeder
twitter: @hassan

Colin Law

unread,
Jan 31, 2015, 3:33:31 PM1/31/15
to rubyonra...@googlegroups.com
As I guess you are a beginner to Rails, I suggest you work right
through a good tutorial such as railstutorial.org (which is free to
use online). That will show you the basics of rails and should answer
your question, and a lot more that you will need to ask.

Colin
Reply all
Reply to author
Forward
0 new messages