2016-01-26 52 views
0

我目前正在使用Public_Activity進行用戶通知。問題在於,如果沒有Public :: Activity導致500內部服務器錯誤,我無法創建新帖子。該錯誤是在我的創造方法發生了Posts_Controller.rb接收未知屬性錯誤公共活動Rails 4

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> 
+0

請顯示您的'post_params'方法 – sjagr

+0

我在更新的問題中包含了帖子參數。 –

回答

1

我通過將自定義字段添加到活動表中解決了此問題。