0
我創建了一個活動模型,並試圖讓它在用戶刪除其活動時也刪除相應的狀態。刪除狀態時,我已經可以做到這一點,它會刪除活動,但不知道如何在相反的方向進行操作。我試圖通過targetable_id找到狀態,但我得到:未定義的方法'find_by_targetable_id'rails活動
undefined method `find_by_targetable_id' for #<Class:0x8df4a70>
Parameters:
{"_method"=>"delete",
"authenticity_token"=>"s2wKOZxCBVarT5uge3AIFNXHepFuvNGM+kU/q+ArOjA=",
"id"=>"18"}
如果你熟悉public_activity寶石則定位的是同樣的事情,可跟蹤並在這個例子中,活動ID是18和它對應的狀態ID是53
ActivitiesController
class ActivitiesController < ApplicationController
before_filter :authenticate_member!, only: [:destroy]
before_filter :find_activity, only: [:destroy]
def index
following_ids = current_member.following_members.map(&:id)
@activities = Activity.where("member_id in (?)", following_ids.push(current_member.id)).order("created_at desc").all
end
def destroy
@status = Activity.targetable
if @status
@status.destroy
end
@activity.destroy
respond_to do |format|
format.html { redirect_to :back }
format.json { head :no_content }
end
end
private
def find_activity
@activity = current_member.activities.find(params[:id])
end
end
StatusesController
class StatusesController < ApplicationController
before_filter :authenticate_member!, only: [:new, :create, :edit, :update, :destroy]
before_filter :find_member
before_filter :find_status, only: [:edit, :update, :destroy, :show]
rescue_from ActiveRecord::RecordNotFound do
render file: 'public/404', status: 404, formats: [:html]
end
# GET /statuses
# GET /statuses.json
def index
@statuses = Status.order('created_at desc').all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @statuses }
end
end
# GET /statuses/1
# GET /statuses/1.json
def show
respond_to do |format|
format.html # show.html.erb
format.json { redirect_to profile_path(current_member) }
end
end
# GET /statuses/new
# GET /statuses/new.json
def new
@status = Status.new
@status.build_document
respond_to do |format|
format.html # new.html.erb
format.json { render json: @status }
end
end
# GET /statuses/1/edit
def edit
end
# POST /statuses
# POST /statuses.json
def create
@status = current_member.statuses.new(params[:status])
respond_to do |format|
if @status.save
current_member.create_activity(@status, 'created')
format.html { redirect_to :back }
format.json
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 }
end
end
end
# PUT /statuses/1
# PUT /statuses/1.json
def update
if params[:status] && params[:status].has_key?(:user_id)
params[:status].delete(:user_id)
end
respond_to do |format|
if @status.update_attributes(params[:status])
format.html { redirect_to profile_path(current_member), notice: 'Status was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @status.errors, status: :unprocessable_entity }
end
end
end
# DELETE /statuses/1
# DELETE /statuses/1.json
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 :back }
format.json { head :no_content }
end
end
private
def find_member
@member = Member.find_by_user_name(params[:user_name])
end
def find_status
@status = current_member.statuses.find(params[:id])
end
def sortable_date
created_at
end
end
是的,我只是注意到這一點,並修復它。謝謝。 – iamdhunt
其實我如何才能使這隻在targettable_type是「狀態」時才被調用? – iamdhunt
沒關係,我知道了 – iamdhunt