Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Having trouble getting my tags page to properly render selected microposts.

rails 4 html erb views controllers

  • Please log in to reply
No replies to this topic

#1 j.whittaker

j.whittaker

    Passenger

  • Members
  • 5 posts

Posted 21 March 2014 - 08:01 PM

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.
Everything is working ok, but if I click on one of my tags in one of my microposts, I can only figure out how to render it very simply.

What I want is for the rendered tagged.html.erb page to look just like the _feed_item.html.erb page, just filtered for whatever tag you clicked on on the previous page.

Relevant source files:

    Gemfile:
    
        ...
        gem 'acts-as-taggable-on' , '~> 2.4.1'
        ...
    
    Routes.rb
        ...
        match 'tagged',   to: 'microposts#tagged',    :as => 'tagged', via: 'get'
        match 'tags/:tag', to: 'microposts#tagged', as: :tag, via: 'get'
        end
    
    Microposts_helper.rb
        module MicropostsHelper
        include ActsAsTaggableOn::TagsHelper
        ...
    
    _micropost.html.erb
        <li>
        ...
        <span class="tags">
            <%= micropost.tag_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]  
    
          # 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, "Tags (separated by commas):" %>
            <%= 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"], name: "index_taggings_on_tag_id"
      add_index "taggings", ["taggable_id", "taggable_type", "context"], name: "index_taggings_on_taggable_id_and_taggable_type_and_context"
    
      create_table "tags", force: true do |t|
        t.string "name"
      end
         ....
    _feed_item.html.erb
    ...
    <span class="tags">
            Tags: <%= raw feed_item.tag_list.map { |t| link_to t, tag_path(t) }.join(', ') %>
        </span><br />
    ...
    tagged.html.erb
    
    <%= render @microposts %>

 

---

 

What else do I need to add to the tagged.html.erb page?  Also, do I need to add anything to the controller?

I've tried googling around to fix this problem with no luck.  I used some code from this railscast: http://railscasts.co...des/382-tagging

Thanks much for your help.







Also tagged with one or more of these keywords: rails 4, html, erb, views, controllers

1 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users


    Yahoo (1)