0

我正在创建一个“标签”字段供用户在使用 act-as-taggable-on 创建引脚时输入。我已经运行了迁移并编辑了 form_html.erb、pins_controller.rb 和 pin.rb。

标签似乎没有保存,当我打开帖子进行编辑时,它们已经消失了。

这是我的 form_html.erb

   <%= 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

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

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

关于为什么我似乎无法保存/输入标签的任何想法?

4

1 回答 1

0

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

于 2015-05-10T15:51:08.237 回答