标签没有保存或显示,为什么?
|
我正在使用行为上可标记的方式,并且很幸运,暂时没有尝试解决这个问题。由于某种原因,我无法让标签显示或保存在数据库中。我确实需要一些帮助,我在网上看了一下,而且文档是中级的或不友好的。这是我的代码:
设计用户模型:
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)执行诸如create,destroy,update标签等操作。
用户模型:
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
products / index.html.erb:
<td><%= product.tag_list %></td>
products / show.html.erb:
<p>
<b>Tags:</b>
<%= @product.tag_list %>
</p>
没有找到相关结果
已邀请:
2 个回复
鞋扣蚊冈借
蜗仓馈
除此之外,没有抛出任何其他错误,幸运的是我也想到了这一点。