我遇到了最奇怪的问题,我不知道如何解决。我的模型具有与之关联的多态活动模型,该模型在用户创建状态,媒体等时创建活动。在我的本地版本,甚至在我的forkoku应用程序上,一切都工作正常。但是,当我在主应用程序上删除一个项目时,它不会删除相应的活动。它仅在主应用程序上发生,我的本地构建和分叉应用程序均会按预期删除活动。他们都使用相同的代码。我不知道为什么它不起作用。
statuses_controller.rb
class StatusesController < ApplicationController
before_filter :find_status, only: [:edit, :update, :destroy]
def create
@status = current_member.statuses.new(params[:status])
respond_to do |format|
if @status.save
@activity = current_member.create_activity(@status, 'created')
format.html { redirect_to :back }
format.json
format.js
else
format.html { redirect_to profile_path(current_member), alert: 'Post wasn\'t created. Please try again and ensure image attchments are under 10Mbs.' }
format.json { render json: @status.errors, status: :unprocessable_entity }
format.js
end
end
end
def destroy
@activity = Activity.find_by_targetable_id(params[:id])
if @activity
@activity.destroy
end
@status.destroy
respond_to do |format|
format.html { redirect_to profile_path(current_member) }
format.json { head :no_content }
end
end
def find_status
@status = current_member.statuses.find(params[:id])
end
end
member.rb
class Member < ActiveRecord::Base
def create_activity(item, action)
activity = activities.new
activity.targetable = item
activity.action = action
activity.save
activity
end
end
移民
class CreateActivities < ActiveRecord::Migration
def change
create_table :activities do |t|
t.integer :member_id
t.string :action
t.integer :targetable_id
t.string :targetable_type
t.timestamps
end
add_index :activities, :member_id
add_index :activities, [:targetable_id, :targetable_type]
end
end
我想通了。我删除了before_filter :find_status
上destroy
,并将此相反:@status = Status.find(params[:id])
。不知道为什么以前的方法行不通,但是在所有构建中都行得通。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句