私はacts-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>