Я использую actions-as-taggable-on и без всякой удачи пытался выяснить это некоторое время. По какой-то причине я не могу заставить свои теги отображаться или сохранять внутри базы данных. Мне действительно нужна помощь, я посмотрел онлайн, и документация промежуточная или не новичка. Вот мой код:
Разработка пользовательской модели:
class User < ActiveRecord::Base
has_many :products, :dependent => :destroy
acts_as_tagger
end
Модель продуктов:
class Product < ActiveRecord::Base
attr_accessible :name, :date, :price, :tag_list
acts_as_taggable_on :tags
belongs_to :user
end
Форма:
<div class="field">
<%= f.label :tag_list %>
<%= f.text_field :tag_list %>
</div>
Показать представление:
<p>
<b>Tags:</b>
<%= @product.tag_list %>
</p>
РЕДАКТИРОВАТЬ С ОБНОВЛЕННЫМ И РАБОЧИМ КОДОМ:
Я использую Devise и позволяю только current_user (user_id в таблице продуктов) выполнять такие действия, как создание, уничтожение, обновление тегов и т. Д.
Модель пользователя:
class User < ActiveRecord::Base
has_many :products, :dependent => :destroy
acts_as_tagger
end
Модель продукта:
class Product < ActiveRecord::Base
attr_accessible :name, :date, :price, :tag_list, :longitude, :latitude
acts_as_taggable_on :tags
end
Контроллер продуктов:
class ProductsController < ApplicationController
before_filter :authenticate_user!
def index
@products = Product.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @products }
end
end
def show
@product = Product.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @product }
end
end
def new
@product = Product.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @product }
end
end
def edit
@product = current_user.products.find(params[:id])
@product.user = current_user
end
def create
@product = current_user.products.build(params[:product])
@product.user = current_user
respond_to do |format|
if @product.save
format.html { redirect_to(@product, :notice => 'Product was successfully created.') }
format.xml { render :xml => @product, :status => :created, :location => @product }
else
format.html { render :action => "new" }
format.xml { render :xml => @product.errors, :status => :unprocessable_entity }
end
end
end
def update
@product = current_user.products.find(params[:id])
@product.user = current_user
respond_to do |format|
if @product.update_attributes(params[:product])
format.html { redirect_to(@product, :notice => 'Product was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @product.errors, :status => :unprocessable_entity }
end
end
end
def destroy
@product = current_user.products.find(params[:id])
@product.destroy
respond_to do |format|
format.html { redirect_to(products_url) }
format.xml { head :ok }
end
end
end
Продукты / index.html.erb:
<td><%= product.tag_list %></td>
Продукты / show.html.erb:
<p>
<b>Tags:</b>
<%= @product.tag_list %>
</p>