我正在使用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>
ERB對於這種形式看起來是什麼樣的,控制器看起來像什麼產品? –
我更新了它以向我展示我的產品控制器。 – LearningRoR
像這樣的'#@ product = Product.new(params [:product])是否在您的應用中註釋掉?這意味着什麼都沒有得救。如果這是你評論他們的原因,你需要修復你的設計/康康角色。 –