Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails: Acts as taggable on gem, working with context in model

I was wondering if someone can help me understand this part in the documentation:

With the defined context in model, you have multiple new methods at disposal to manage and view the tags in the context. For example, with :skill context these methods are added to the model: skill_list(and skill_list.add, skill_list.remove skill_list=), skills(plural), skill_counts.

I have this:

model:

class Project < ActiveRecord::Base
  acts_as_taggable # Alias for acts_as_taggable_on :tags
  acts_as_taggable_on :item
end

controller:

def project_params
  params.require(:project).permit(
    :user_id, :tag_list)
end

view

<%= f.text_field :tag_list %> <!-- wrapped in a simple_form -->

So my question is... does that mean, if I have :items in my model, I can replace all the :tag_list into :item_list? And just use item_list from now on? I tried this, but its not producing the same results as what I currently have... maybe I messed up somewhere, but is this "theoretically" correct?

Thanks

BONUS:

So eventually, if I have more than one thing that I want to tag:

acts_as_taggable_on :item, :more_taggable_item

I can have this in my strong params

params.require(:project).permit(
    :user_id, :tag_list, :more_taggable_item_list)

and then I can use it in my view:

<%= f.text_field :more_taggable_item_list %>
like image 722
hellomello Avatar asked Jul 18 '15 21:07

hellomello


1 Answers

I guess you have to use the plural form, as in the docs. So :items instead of :item and :more_taggable_items instead of :more_taggable_item. Rails offers a lot of functionality to use for gems when it comes to creating proper naming convetions and good gems make use of this.

# model
class Project < ActiveRecord::Base
  acts_as_taggable_on :items, :more_taggable_items
end

# params
def project_params
  params.require(:project).permit(
    :user_id, :item_list, :more_taggable_item_list)
end

# view
<%= f.text_field :item_list %> 
<%= f.text_field :more_taggable_item_list %>
like image 157
smallbutton Avatar answered Oct 09 '22 09:10

smallbutton