public-activity / public_activity

Easy activity tracking for models - similar to Github's Public Activity
MIT License
2.96k stars 334 forks source link

I'm getting an ActiveRecord:UnknownAttributeError #250

Closed RailsCod3rFuture closed 8 years ago

RailsCod3rFuture commented 8 years ago

ActiveRecord::UnknownAttributeError (unknown attribute 'photo' for PublicActivity::Activity.): in 'create'

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">]

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

if @post.save
  flash[:success] = "You've Added A New Post"
  redirect_to @post
else
  flash[:danger] = @post.errors.full_messages.to_sentence
  redirect_to root_url
end

end

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

def find_post @post = Post.find(params[:id]) end

def correct_user @post = current_user.posts.find_by(id: params[:id]) redirect_to root_url if @post.nil? end

def without_tracking Post.public_activity_off yield if block_given? Post.public_activity_on end

Post.rb 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 }

RailsCod3rFuture commented 8 years ago

I'm using CarrierWave for uploading photos.

farnoy commented 8 years ago

this looks to be the reason: photo: proc {|controller, model| model.photo }, What are you trying to do with this?

RailsCod3rFuture commented 8 years ago

Is it necessary to have? Can I just use the tracked method instead? Check my view for listing activities below. Can you provide me with the best approach if this is a tad bit overkill?

Notifications.html.erb <%= render_activities(@activities) %>

_activities.html.erb

    <% @activities.each do |activity| %>
  • <%= activity.created_at.strftime('%H:%M:%S %-d %B %Y') %>
    <%= activity.owner ? activity.owner.username : 'Guest' %> <%= render_activity(activity, display: :i18n) %> <% if activity.trackable %> "<%= link_to activity.trackable.body, post_path(activity.trackable) %>" "<%= link_to(image_tag(activity.trackable.photo.url(:medium)), post_path(activity.trackable)) %>" <% elsif activity.photo %> "<%= activity.photo %>" <% else %> with unknown post <% end %>
  • <% end %>
farnoy commented 8 years ago

You added photo attachment on your model but haven't modified Activity to have a photo, so you cannot assign it from tracked. If you want carrierwave attachments on your Activities, follow this guide: https://github.com/chaps-io/public_activity/wiki/%5BHow-to%5D-Use-custom-fields-on-Activity.

pokonski commented 8 years ago

Yeah, like @farnoy said. You are trying to assign a photo to Activity, and activity doesn't have that method, which comes from Carrierwave (and having some columns on that table)

Closing as it's not a bug.