首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用act-as-taggable-on将标签添加到引脚而不保存标签

使用act-as-taggable-on将标签添加到引脚而不保存标签
EN

Stack Overflow用户
提问于 2015-05-10 18:49:33
回答 1查看 67关注 0票数 0

当使用act-as-taggable-on创建管脚时,我创建了一个'tags‘字段供用户输入。我已经运行了迁移并编辑了form_html.erb、pins_controller.rb和pin.rb。

标签似乎没有保存,当我打开一篇文章进行编辑时,它们已经显示为灰色。

这是我的form_html.erb

代码语言:javascript
复制
   <%= form_for @pin, html: { multipart: true } do |f| %>
 <% if @pin.errors.any? %>
   <div id="error_explanation">
    <h2><%= pluralize(@pin.errors.count, "error") %> prohibited this pin from being saved:</h2>

    <ul>
    <% @pin.errors.full_messages.each do |message| %>
    <li><%= message %></li>
  <% end %>
     </ul>
   </div>
 <% end %>

 <div class="form-group">
   <%= f.label :image %>
   <%= f.file_field :image, class: "form-control" %>
  </div>

  <div class="form-group">
<%= f.label :description %><br>
<%= f.text_field :description, class: "form-control" %>
  </div>
  <div class="form-group">
<%= f.label :date %><br>
<%= f.date_field :date, class: "form-control" %>
  </div>
  <div class="form-group">
  <%= f.label :tags, "Tags (separated by commas)"%><br />
  <%= f.text_field :tags_list%>
</div>
 <div class="actions">
    <%= f.submit %>
  </div>
 <% end %>

我的pins_controller.rb

代码语言:javascript
复制
class PinsController < ApplicationController
  before_action :set_pin, only: [:show, :edit, :update, :destroy]
  before_action :correct_user, only: [:edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show]



 def index
   @pins = Pin.all.order("created_at DESC").paginate(:page => params[:page], :per_page => 8)
 end

  def show
  end

  def new
    @pin = current_user.pins.build

  end

  def edit
  end

  def create
    @pin = current_user.pins.build(pin_params)
    if @pin.save
      redirect_to @pin, notice: 'Pin was successfully created.'
    else
      render action: 'new'
    end
  end

  def update
    if @pin.update(pin_params)
      redirect_to @pin, notice: 'Pin was successfully updated.'
    else
      render action: 'edit'
    end
  end

  def destroy
    @pin.destroy
    redirect_to pins_url
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_pin
      @pin = Pin.find(params[:id])
    end

        def correct_user
      @pin = current_user.pins.find_by(id: params[:id])
      redirect_to pins_path, notice: "Not authorized to edit this pin" if @pin.nil?
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def pin_params
      params.require(:pin).permit(:description, :image, :date, :tags)
    end
end

我的pin.rb

代码语言:javascript
复制
class Pin < ActiveRecord::Base
     belongs_to :user
     has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }
     validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png"]
     acts_as_taggable
end

为什么我看起来不能保存/输入标签?

EN

回答 1

Stack Overflow用户

发布于 2015-05-10 23:51:08

我意识到我需要在pin.rb和pin_controller.rb中使用tag_list,这解决了这个问题!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30150504

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档