user3174983
user3174983

Reputation: 85

Rails 4 Adding Tags to a Micropost, having routing issues; Railstutorial add ons

I am working on building an extension off of Hartl's Rails tutorial micropost app, and trying to add tagging with the Acts as Taggable on Gem.

I have followed some instructions but am now getting a routing erorr:

Routing Error uninitialized constant MicropostsHelper::ActsAsTaggableOn

app/helpers/microposts_helper.rb:2:in `<module:MicropostsHelper>'
app/helpers/microposts_helper.rb:1:in `<top (required)>'
app/controllers/application_controller.rb:1:in `<top (required)>'
app/controllers/static_pages_controller.rb:1:in `<top (required)>'

Here is my code, let me know if you see any reason why this is not working.

Gemfile:

    ...
    gem 'acts-as-taggable-on' , '~> 2.4.1'
    ...

Routes.rb
    ...
    match 'tagged',   to: 'microposts#tagged',    :as => 'tagged', via: 'get' 
    end

Microposts_helper.rb
    module MicropostsHelper
    include ActsAsTaggableOn::TagsHelper
    ...

_micropost.html.erb
    <li>
    ...
    <span class="tags">
        <%= micropost.tags_list %>
    ...
    </li>

micropost.rb
    class Micropost < ActiveRecord::Base

      belongs_to :user
      acts_as_taggable #tags, this line and below
      acts_as_taggable_on :tags
      default_scope -> { order('created_at DESC')  
    ...

Microposts_controller.rb
    class MicropostsController < ApplicationController
      before_action :signed_in_user, only: [:create, :destroy]
      before_action :correct_user,   only: [:destroy]  #add in edit here

      # for tagging:
      def index
      if params[:tag]
          @microposts = Micropost.tagged_with(params[:tag])
        else
          @microposts = Micropost.all
        end
      end

      def create
        @micropost = current_user.microposts.build(micropost_params)
        if @micropost.save
          flash[:success] = "Gif post created!"
          redirect_to root_url
        else
          @feed_items = []
          render 'static_pages/home'

        end
      end

    ...

      def tagged #more for tagging
        if params[:tag].present?
          @microposts = Micropost.tagged_with(params[:tag])
        else
         @microposts = Micropost.postall
        end
      end

_micropost_form.html.erb
    <%= form_for(@micropost) do |f| %>
    <%= render 'shared/error_messages', object: f.object %>
    <div class='field'>
        <%= f.text_area :content, placeholder: "Post your message..." %>
        <%= f.label :tags %>
        <%= f.text_field :tag_list %>
    ....

schema.rb
     ....
     create_table "taggings", force: true do |t|
     t.integer  "tag_id"
     t.integer  "taggable_id"
     t.string   "taggable_type"
     t.integer  "tagger_id"
     t.string   "tagger_type"
     t.string   "context",       limit: 128
     t.datetime "created_at"
     end

     add_index "taggings", ["tag_id", "taggable_id", "taggable_type", "context", "tagger_id", "tagger_type"], name: "taggings_idx", unique: true

     create_table "tags", force: true do |t|
     t.string "name"
     end

     add_index "tags", ["name"], name: "index_tags_on_name", unique: true
     ....

That's the long and short of it. I've tried googling around to fix this problem with no luck. Thanks much for your help.

Upvotes: 0

Views: 152

Answers (1)

Kirti Thorat
Kirti Thorat

Reputation: 53038

Make sure to restart the rails server after installing the gem. It looks like the environment is not loaded so kill the running server and start it again.

Upvotes: 1

Related Questions