Multilingual tagging in Rails - ruby-on-rails

I want to add tags to a model and I also want those tags to be translatable to n languages.
I'm already using i18n gem to translate the contents. What's a proper way to implement translatable tags? Of course those tags will be created by different users.
The only way I thought about was to create a Tag model with {:id, :translation_id, :language, :name} using translation_id to link the tag to the original one.

Maybe the Globalize 3 gem could do the job for you.

Related

twitter bootstrap rails fields_for equivalent

I am working with a Rails 6.1 project and am using the twitter-bootstrap-rails gem to create a form using bootstrap_form_for for an object that has an association.
I would like to add the association fields to the form, and am finding that Rails has a fields_for, helper but I am not finding any equivalent in twitter-bootstrap-rails.
I was successfully using the fields_forhelper until I needed to create a form_group in it for radio buttons, where the data for the group is coming from an array of hashes.
Although the fields_forhelper does have the ability to create radio button groups, I am not seeing how to use a hash to populate the radio button fields.
How does one add form fields for an object's association, when using the twitter-bootstrap-rails gem maintaining the the full form_group capabilities?
If this isn't possible, can anyone point me to documentation on creating a radio button group under fields_for using an array of hashes as the data source?
Many thanks in advance for your suggestions.

Use tags in Rails

I have 3 tables a monster and a tag table, and a monster_tag table in between. I want to be able to show all the monsters that share the same tag by reading the monster_tag table.Would it be better if I wrote the feature from scratch, or use the act-as-taggable gem. If I pick the act-as-taggable gem, how would I configure it to use the monster_tag table.
For functionality you described setting up has_many through relationships should be enough.
You can get all monsters that share the same tag with something like Tag.find(name: 'tag').monsters

Rails tagging gem that supports tagging a model with other models?

Most Rails tagging gems support tagging an object with arbitrary strings. (In addition to this) I'd like to tag an object with other objects.
Imagine I am building a Q&A site for Dog Lovers: I want a person to be able to ask a question. The person can then tag that question with arbitrary strings (as every tagging gem supports), BUT if the question is about a particular dog breed or dog breed(s), I'd like the user to additionally be able to tag the question with dog breeds. Dog breeds are a model in my web application, with lots of metadata and associations of their own.
All Rails tagging gems I've been able to find simply regard tags as words; I want to know if there is a gem out there that supports tags being objects/models themselves.
I certainly know I can roll my own through just writing some has_many association logic, but what's nice about a lot of the Rails taggable gems is that they come with loads of methods that help querying and displaying the tag data very easy.
I don't know of any that do exactly what you are asking, but the acts_as_taggable_on gem does support tags in different contexts. Maybe you could create a tag context for dog breed, allow the choice of breed based on your model and then use the name or id for the breed model record as your tag within the breed context. You can find more information here but you would add something like this to your model:
acts_as_taggable #for your normal tags
acts_as_taggable_on :breeds
Then you could find items tagged with a breed using:
Question.tagged_with('border collie', :on => :breeds)
I know this isn't exactly what you were looking for, but this allows you to use the find methods of an existing gem while still allowing you to segment out your tags the way you described.

Using a select field with Rails nested object forms

Is it possible to use select fields with nested object forms feature of Rails 2.3?
Example:
Suppose you have a Article model, Category model, and a ArticleCategories join model. Article has_many Categories through ArticleCategories.
On our Edit Article form, you want to have an HTML select list of all the available categories. The user can select one or more Category names to assign to the Article (multiple select is enabled).
There are lots of ways to do this, but I'm wondering if there is a simple way to accomplish this using the nested objects feature. What would the form look like in your view?
Check out the nested form example from Github:
http://github.com/alloy/complex-form-examples
It's been a while since I looked at it, so I'm not sure if it covers exactly what you wanna do, but its a nice source for ideas / patterns.
Assuming you have defined the models and their relationships so you can do this:
#art = Article.find(article_id)
#art.categories # returns list of category objects this article is assigned to.
Then I usually use http://trendwork.kmf.de/175
You need to copy the JavaScript file into public/javascripts but after that you can just create the form element with something like:
swapselect(:article,#art,:categories,Category.find(:all).map { |cat| [cat.name, cat.id] })
(I would tend to wrap that in a helper to make the call even cleaner)
One small gotcha is that for very long lists it can run a little slow in IE6 because there's quite a lot of appendChild calls in the js which is notorioulsy slow in IE6
Update: Apologies. This doesn't really answer your original question, which was specifically about the Rails 2.3 feature. The swapselect option is version independent and doesn't make use of newer Rails functionality.

Adding tags to posts in Ruby on Rails

I am creating a blog in Rails using Scaffolding. I want to add a 'tags' field on each post like on StackOverflow and WordPress. I can do this with the string type ( rails generate scaffold post title:string body:text tags:string ) and then comma separated, but it's not good practice since I want the reader to browse by tags ( e.g. /tags/web20 or /tags/lol ). How can I do this?
Tagging is so common that implementations are a commodity. I believe "acts as taggable on" is usually the preferred way of implementing tags.
See other popular solutions here.
If you wish to implement it yourself, you could dive into the source code to find some ideas.
I would suggest creating a Tag model and using has_and_belongs_to_many to assign tags to posts. I don't know if the scaffold feature will help you create a form for that, but it shouldn't be difficult to add it yourself. I also suggest using the formtastic plugin as it's much easier and nicer to create forms with it.
Err, the usual way? Add Tag entity, add has_many :tags in your Post entity. Then migrate. That would be all.

Resources