Rails活动破坏记录不工作

编程入门 行业动态 更新时间:2024-10-24 22:30:37
本文介绍了Rails活动破坏记录不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有最奇怪的问题,我不知道如何解决它。我的模型有一个多态活动模型与他们相关联,创建活动时,用户创建一个状态,媒体等。一切都在我的本地构建,甚至在我的分叉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 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])。不知道为什么以前的方式没有工作,但这适用于所有的构建。

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活动破坏记录不工作

本文发布于:2023-10-27 16:05:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1533870.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:工作   Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!