2011-06-18 45 views
0

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

ERB對於這種形式看起來是什麼樣的,控制器看起來像什麼產品? –

+0

我更新了它以向我展示我的產品控制器。 – LearningRoR

+0

像這樣的'#@ product = Product.new(params [:product])是否在您的應用中註釋掉?這意味着什麼都沒有得救。如果這是你評論他們的原因,你需要修復你的設計/康康角色。 –

回答

1

嘗試在視圖中使用此代碼:

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

我會給你信用的,因爲它讓我回答! – LearningRoR

1

我遇到類似的事情。我沒有補充說: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 

沒有拋出任何其他錯誤重要的是,幸運的是我也想到了這。

相關問題