标签不保存或显示,为什么?(Tags not saving or showing, why?)

编程入门 行业动态 更新时间:2024-10-25 11:18:39
标签不保存或显示,为什么?(Tags not saving or showing, why?)

我现在正在使用行为标签 -并且没有任何运气试图找出一段时间。 由于某种原因,我无法让我的标签出现或保存在数据库中。我真的需要一些帮助,我在网上查看,文档是中间或不是新手友好的。 这是我的代码:

设计用户模型:

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>

I am using acts-as-taggable-on and without any luck been trying to figure this out for awhile now. For some reason i cannot get my tags to show up or save inside of the database.I really need some help, i looked online and the documentation is intermediate or not newbie friendly. Here is my code:

Devise User Model:

class User < ActiveRecord::Base has_many :products, :dependent => :destroy acts_as_tagger end

Products Model:

class Product < ActiveRecord::Base attr_accessible :name, :date, :price, :tag_list acts_as_taggable_on :tags belongs_to :user end

Form:

<div class="field"> <%= f.label :tag_list %> <%= f.text_field :tag_list %> </div>

Show View:

<p> <b>Tags:</b> <%= @product.tag_list %> </p>

EDIT WITH UPDATED AND WORKING CODE:

I am using Devise and letting only the current_user (user_id in products table) do actions such as create,destroy,update tags,etc.

User Model:

class User < ActiveRecord::Base has_many :products, :dependent => :destroy acts_as_tagger end

Product Model:

class Product < ActiveRecord::Base attr_accessible :name, :date, :price, :tag_list, :longitude, :latitude acts_as_taggable_on :tags end

Products Controller:

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>

最满意答案

尝试在您的视图中使用此代码:

<p> <b>Tags:</b> <%= @product.tags %> </p>

Try using this code in your view:

<p> <b>Tags:</b> <%= @product.tags %> </p>

更多推荐

本文发布于:2023-08-03 15:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1395138.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不保存   标签   Tags   saving   showing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!