Receiving unknown attribute error Public Activity Rails 4

479 views Asked by At

I'm currently using Public_Activity for user notifications. The problem is that I cannot create a new post without Public::Activity causing a 500 internal Server error. The error is occurring in my create method for the Posts_Controller.rb

ActiveRecord::UnknownAttributeError (unknown attribute 'photo' for PublicActivity::Activity.):
  app/controllers/posts_controller.rb:28:in `create'

  def create
    @user = current_user
    @post = @user.posts.build(post_params)

    if @post.save
      flash[:success] = "New Moment Posted"
      redirect_to @post
    else
      flash[:danger] = @post.errors.full_messages.to_sentence
      redirect_to root_url
    end
  end

post.rb

class Post < ActiveRecord::Base
  include PublicActivity::Model

  tracked owner: Proc.new {|controller, model| controller.current_user ? controller.current_user : nil },
          photo: proc {|controller, model| model.photo },
          body: proc {|controller, model| model.body }

  default_scope -> { order(created_at: :desc) }

  validates :body, presence: true, length: {maximum: 150}
  validates :photo, presence: true #CarrierWave

  mount_uploader :photo, PhotoUploader

  has_many :comments, as: :commentable, dependent: :destroy
  acts_as_commentable
  belongs_to :user
end

Cannot render console with content type multipart/form-dataAllowed content types: [#<Mime::Type:0x3b50128 @synonyms=["application/xhtml+xml"], @symbol=:html, @string="text/html">, #<Mime::Type:0x3b4b610 @synonyms=[], @symbol=:text, @string="text/plain">, #<Mime::Type:0x3b430d8 @synonyms=[], @symbol=:url_encoded_form, @string="application/x-www-form-urlencoded">]

Posts_Controller.rb Parameters

def post_params
    params.require(:post).permit(:body, :photo, :video)
end

show view for activities

<div class="col-sm-3">
  <ul class="list-group">
    <% @activities.each do |activity| %>
        <li class="list-group-item">
          <span class="glyphicon glyphicon-<%= activity.key.match(/\.(.*)/)[1] %>"></span>
          <small class="text-muted"><%= activity.created_at.strftime('%H:%M:%S %-d %B %Y') %></small><br/>
          <strong><%= activity.owner ? activity.owner.username : 'Guest' %></strong>
          <%= render_activity(activity, display: :i18n) %>
          <% if activity.trackable %>
              "<%= link_to activity.trackable.body, post_path(activity.trackable) %>"
              "<%= link_to activity.trackable.photo, post_path(activity.trackable) %>"
          <% elsif activity.photo %>
              <span class="text-muted">"<%= activity.photo %>"</span>
          <% else %>
              with unknown post
          <% end %>
        </li>
    <% end %>
  </ul>
</div>
1

There are 1 answers

0
Daniel W On

I solved this problem by adding a custom field to the activities table.