0

我目前正在使用 Public_Activity 进行用户通知。问题是如果没有 Public::Activity 导致 500 内部服务器错误,我无法创建新帖子。该错误发生在我的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 参数

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

显示活动视图

<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>
4

1 回答 1

1

我通过向活动表添加自定义字段解决了这个问题。

于 2016-01-27T01:32:09.713 回答