开发者

Parsing blog post tags from a text_field

开发者 https://www.devze.com 2022-12-18 05:45 出处:网络
Ok, so you know how you ask a question here, and in the \"Tags\" field you can enter several space-separated tags into a sin开发者_如何学运维gle text field?

Ok, so you know how you ask a question here, and in the "Tags" field you can enter several space-separated tags into a sin开发者_如何学运维gle text field?

I'm trying to replicate similar behavior in my Rails app. Except instead of questions, I'm doing a blog app (which has "posts"), and tagging those.

I'm using "form_for" to build the quick form. Inside of that I have the line:

f.text_field :tags

The problem I'm running into is, "tags" is not a field on my Post class. My Post class HABTM tags. So, somehow I need to parse the tags text field (using String.split), and pass the resulting tag Strings into my controller, so my controller can create and associated the tags along with the new blog post.

Is using "form_for" not going to work in this case? Is doing this sort of behavior beyond the design of the quick-and-dirty "form_for" functionality?

Thanks!


Unless you really want to reinvent the wheel, I would suggest using a plugin for this. ActsAsTaggableOnSteroids is a mature one. http://svn.viney.net.nz/things/rails/plugins/acts_as_taggable_on_steroids


Agree with Ben on this - there's lots of great plugins and features/helpers that make them simple to use. And you can learn a lot about how to do this in a well-designed way. Here's another good choice.

http://github.com/mbleigh/acts-as-taggable-on

0

精彩评论

暂无评论...
验证码 换一张
取 消