How to use jquery-Tokeninput and Acts-as-taggable-on
You should define a route in your routes.rb
which should handle products/tags
path. You can define it like:
get "products/tags" => "products#tags", :as => :tags
Thus should give you a tags_path
helper which should evaluate to /products/tags
. This should get rid of the errors you mentioned in the question. Be sure to add this route before defining resources :product
in your routes.rb
Now onto acts-as-taggable-on, I haven't used this gem, but you should look at method all_tag_counts
documentation. Your ProductsController#tags
method will need some changes on the following lines. I am not sure if its exactly what would be required, as I use Mongoid and can't test it out.
def tags
@tags = Product.all_tag_counts.(:conditions => ["#{ActsAsTaggableOn::Tag.table_name}.name LIKE ?", "%#{params[:q]}%"])
respond_to do |format|
format.json { render :json => @tags.collect{|t| {:id => t.name, :name => t.name } }
end
end
How to use jquery-Tokeninput and Acts-as-taggable-on
You should define a route in your routes.rb
which should handle products/tags
path. You can define it like:
get "products/tags" => "products#tags", :as => :tags
Thus should give you a tags_path
helper which should evaluate to /products/tags
. This should get rid of the errors you mentioned in the question. Be sure to add this route before defining resources :product
in your routes.rb
Now onto acts-as-taggable-on, I haven't used this gem, but you should look at method all_tag_counts
documentation. Your ProductsController#tags
method will need some changes on the following lines. I am not sure if its exactly what would be required, as I use Mongoid and can't test it out.
def tags
@tags = Product.all_tag_counts.(:conditions => ["#{ActsAsTaggableOn::Tag.table_name}.name LIKE ?", "%#{params[:q]}%"])
respond_to do |format|
format.json { render :json => @tags.collect{|t| {:id => t.name, :name => t.name } }
end
end
Using jquery tokeninput and acts_as_taggable_on
If you still want to use Jquery TokenInput and add tags there are different ways to do it.
1.
This is actually from my same question; the newest answer: How to use jquery-Tokeninput and Acts-as-taggable-on
This could go in your controller.
def tags
query = params[:q]
if query[-1,1] == " "
query = query.gsub(" ", "")
Tag.find_or_create_by_name(query)
end
#Do the search in memory for better performance
@tags = ActsAsTaggableOn::Tag.all
@tags = @tags.select { |v| v.name =~ /#{query}/i }
respond_to do |format|
format.json{ render :json => @tags.map(&:attributes) }
end
end
This will create the tag, whenever the space bar is hit.
You could then add this search setting in the jquery script:
noResultsText: 'No result, hit space to create a new tag',
It's a little dirty but it works for me.
2.
Check out this guy's method: https://github.com/vdepizzol/jquery-tokeninput
He made a custom entry ability:
$(function() {
$("#book_author_tokens").tokenInput("/authors.json", {
crossDomain: false,
prePopulate: $("#book_author_tokens").data("pre"),
theme: "facebook",
allowCustomEntry: true
});
});
3.
Not to sure about this one but it may help: Rails : Using jquery tokeninput (railscast #258) to create new entries
4.
This one seems legit as well: https://github.com/loopj/jquery-tokeninput/pull/219
I personally like the first one, seems easiest to get and install.
ActsAsTaggableOn + jQueryTokenField + Rails 4
I realised that i should have used a text field:
<%= f.text_field :tag_list_tokens, data: {load: @case_law_tags} %>
instead of the simple_form helper <%= f.input :tag_list_tokens, data: {load @case_law_tags} %>
The problem the reloading was related to turbolinks. Rails does not do a complete reload of the document when following a link. It loads the differences between pages through ajax. I fixed the issue by modifying my code as:
case_laws.js.coffee
tokenizeCaseLawTags = ->
$('#case_law_tag_list_tokens').tokenInput '/admin/case_laws/tags.json',
theme: 'facebook'
minChars: 2
allowCustomEntry: true
preventDuplicates: true
prePopulate: $('#case_law_tag_list_tokens').data('load')
$(document).on "ready page:load", ->
tokenizeCaseLawTags()
Jquery Tokeninput & Acts-as-taggable is not working with a parent-child-child nested form
You simply didn't set your @product
variable => it's nil
.
You should show your controller
EDIT:
replace:
<%= p.text_field :tag_list,"data-pre" => @product.tags.map(&:attributes).to_json %>
with:
<%= p.text_field :tag_list,"data-pre" => p.object.tags.map(&:attributes).to_json %>
This should work for edit
as well.
It's really good sense here: you can't invoke something you didn't set.
Related Topics
What's the Best Way to Talk to a Database While Using Sinatra
Can't Get Rack-Cors Working in Rails Application
Bundle Can't Install Rmagick Gem on MAC Osx 10.7
Is There a Literal Notation for an Array of Symbols
How to Run Rails Console in the Test Environment and Load Test_Helper.Rb
How to Find an Actively Developed Lint Tool for Ruby
Nested Object Creation with JSON in Rails
Rails Search with Optional Parameters
What's the Difference Between the Build and Create Methods in Factorygirl
How to Return Something Early from a Block
Cannot Execute "Rails Console" Due to an Error with Readline
How to Write a Shell Script That Starts Tmux Session, and Then Runs a Ruby Script
How to Check If a Class Is Defined
How to Handle Errors with Httparty
Scope of Constants in Ruby Modules