Sachin Karia
Sachin Karia

Reputation: 565

Adding tags to pins not saving tags using act-as-taggable-on

I am creating a 'tags' field for users to enter when creating pins using using act-as-taggable-on. I have run the migration and edited the form_html.erb, pins_controller.rb, and pin.rb.

The tags don't seem to save and when I open a post to edit they have diasappeared.

Here is my 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 %>

My 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

My 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

Any ideas to why I can't seem to save/input tags?

Upvotes: 0

Views: 81

Answers (1)

Sachin Karia
Sachin Karia

Reputation: 565

I realised that I needed to use tag_list in the pin.rb and pin_controller.rb and this fixed the problem!

Upvotes: 0

Related Questions