【问题标题】:Tags not saving or showing, why?标签没有保存或显示,为什么?
【发布时间】:2014-04-03 12:08:09
【问题描述】:

我正在使用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>

products/show.html.erb:

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

【问题讨论】:

  • 该表单的 ERB 是什么样的,产品的控制器是什么样的?
  • 我更新了它以向您展示我的产品控制器。
  • 您的应用程序中是否已将类似#@product = Product.new(params[:product]) 的行注释掉了?这意味着没有任何东西可以保存。如果这是您将其注释掉的原因,您需要修复您的 devise/cancan 角色。
  • @Devin M 是的,我的角色似乎有问题,所以我取消了他们的注释,他们开始工作,但现在我必须弄清楚我的设计设置,因为 CanCan 都搞砸了。我正在尝试获取它,因此当前用户只能执行上述所有操作。无论如何,感谢德文的帮助!
  • 如果你愿意,可以发布另一个关于 cancan 的问题,我应该能够提供帮助。当它启动时,只需在此处给我一个链接。

标签: ruby-on-rails acts-as-taggable-on


【解决方案1】:

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

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

【讨论】:

  • 我会为此给你功劳,因为它引导我找到答案!
【解决方案2】:

我遇到了类似的事情。我没有将 :tag_list 添加到我的 events_controller 中(我正在为我的青年组制作一个简单的聚会克隆) -

private
        def event_params
            params.require(:event).permit(:title, :text, :user_id, :event_start, :time_begin, :location, :address, **:tag_list**)
        end
end

除此之外没有抛出任何其他错误,幸运的是我也想到了这一点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多