我有最奇怪的问题,我不知道如何解决它。我的模型有一个多态活动模型与他们相关联,创建活动时,用户创建一个状态,媒体等。一切都在我的本地构建,甚至在我的分叉heroku应用程序正常工作。但是,当我删除我的主应用程序上的项目时,它不会删除相应的活动。它只发生在主应用程序,我的本地构建和分叉应用程序删除活动,如预期。他们都使用相同的代码。我不知道为什么它不工作。
I'm having the strangest problem and I don't know how to fix it. My models have a polymorphic activity model associated with them that creates activities when a user creates a status, media, etc. Everything's working fine on my local build and even on my forked heroku app. But when I delete an item on my main app, it doesn't delete the corresponding activity. It only happens on the main app, both my local build and forked app delete the activity as expected. They're all using the same code. I don't know why it's not working.
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 endmember.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])。不知道为什么以前的方式没有工作,但这适用于所有的构建。
I figured this out. I removed the before_filter :find_status on destroy and added this instead: @status = Status.find(params[:id]). Don't know why the previous way didn't work but this works on all builds.
更多推荐
Rails活动破坏记录不工作
发布评论