2016-06-01 43 views
1

多くの類似した投稿を見ましたが、destroyメソッドを使用しようとしたときにこのrecord not foundエラーを取り除くことはできません。問題の2つのモデルはworkouts.rbexercises.rbです。運動has_manyエクササイズ。Railsレコードが見つかりませんエラーIDなしでワークアウトが見つかりませんでした

私は取得していますエラーが私のexercises_controllerから以下のコードの3行目にCouldn't find Workout without an IDです:

def destroy 
    @workout = Workout.friendly.find(params[:workout_id]) 
    exercise = @workout.exercises.find(params[:id]) 

マイexercise.rbモデルは次のとおりです。

class Exercise < ActiveRecord::Base 
    belongs_to :workout 
    belongs_to :user 
    has_many :reports 
    validates :user, presence: true 
end 

workout.rbモデルは次のとおりです。

class Workout < ActiveRecord::Base 
    extend FriendlyId 
    friendly_id :name, use: :slugged 

    belongs_to :user 
    has_many :exercises 
    has_many :reports 
    validates :user, presence: true 
end 

そして私のフルexercises_controllerは次のとおりです。

class ExercisesController < ApplicationController 
    before_action :authenticate_user! 

    def index 
    @exercises = Exercise.all 
    end 

    def new 
    @exercise = Exercise.new 
    end 

    def create 
    @workout = Workout.friendly.find(params[:workout_id]) 
    exercise = @workout.exercises.new(exercise_params) 
    exercise.user = current_user 

    if exercise.save 
     flash[:notice] = "Results saved successfully." 
     redirect_to [@workout] 
    else 
     flash[:alert] = "Results failed to save." 
     redirect_to [@workout] 
    end 
    end 

    def destroy 
    @workout = Workout.friendly.find(params[:workout_id]) 
    exercise = @workout.exercises.find(params[:id]) 

    if exercise.destroy 
     flash[:notice] = "Exercise was deleted successfully." 
     redirect_to [@workout] 
    else 
     flash[:alert] = "Exercise couldn't be deleted. Try again." 
     redirect_to [@workout] 
    end 
    end 

    private 

    def exercise_params 
    params.require(:exercise).permit(:name, :needs_seconds, :needs_weight, :needs_reps) 
    end 

    def authorize_user 
    exercise = Exercise.find(params[:id]) 
    unless current_user == current_user.admin? 
     flash[:alert] = "You do not have permission to create or delete an exercise." 
     redirect_to [exercise.workout] 
    end 
    end 
end 

私のルートは単純です:

resources :workouts 
resources :exercises 

EDIT:

コードの削除を呼び出すには、次のとおりです。

<%= link_to "Delete #{exercise.name}", exercise_path, method: :delete, data: { confirm: 'Are you sure?' } %> 

このエラーがから来ているすべてのアイデア?

+0

'destroy'アクションに来る' params'とは何ですか? – Pavan

+0

ここに来ているリクエストのコードを貼り付けることができます – Mukesh

+0

@Mukesh、元の投稿にERBコードを追加しました。 – Liz

答えて

1

私はコードなしであなたのために多くを行うことはできません。しかし、一般的な間違いはルート上の:idを忘れることです。

は、それがあなたのexerciceリソースの破壊アクションが/exercises/:id

マインドルートでidDELETEアクションになることを忘れがちですので、あなたがRailsのでそれらを自動生成することを見るです!

GET /workouts/:workout_id/exercices(.:format)   exercices#index 
POST /workouts/:workout_id/exercices(.:format)   exercices#create 
GET /workouts/:workout_id/exercices/new(.:format)  exercices#new 
GET /workouts/:workout_id/exercices/:id/edit(.:format) exercices#edit 
GET /workouts/:workout_id/exercices/:id(.:format)  exercices#show 
PATCH /workouts/:workout_id/exercices/:id(.:format)  exercices#update 
PUT /workouts/:workout_id/exercices/:id(.:format)  exercices#update 
DELETE /workouts/:workout_id/exercices/:id(.:format)  exercices#destroy 
GET /workouts(.:format)        workouts#index 
POST /workouts(.:format)        workouts#create 
GET /workouts/new(.:format)       workouts#new 
GET /workouts/:id/edit(.:format)      workouts#edit 
GET /workouts/:id(.:format)       workouts#show 
PATCH /workouts/:id(.:format)       workouts#update 
PUT /workouts/:id(.:format)       workouts#update 
DELETE /workouts/:id(.:format)       workouts#destroy 

:つもりこれらのルートを持つことが可能とされて

resources :workouts do 
    resources :exercices 
end 

はまた、あなたがコントローラ読んで、それはあなたがworkout_idを期待するように、そのための正しいルーティングは次のようになり見えますネストされたルートでworkout_idを気にしてください。

params[:id] = 23 
params[:workout_id] = 3 

:あなたはid = 3でトレーニングを持っている場合

ので、id = 23とexerciceのために、あなたは/workouts/3/exercices/23にし、あなたのExercicesControllerDELETEを送ることができるでしょう、あなたは、これら2つの値にアクセスできるようになりますあなたが期待するように、私は思う。

私は船に行きました、それが役に立つと思います。私はあなたのコードに基づいて私の答えを豊かにしようとしました。

+0

ERBコードを使用して元の投稿を増補しました。このような明白な見落としには申し訳ありません。 – Liz

+0

私はもともとこのようなネストされたルートを持っていましたが、解決しているよりも多くの問題を引き起こしていたので、それらをアンネストしました。 – Liz

+0

どちらの方法でも、あなたのパスのためにあなたの削除ボタンで 'id'を指定する必要があります:' exercise_path(id:exercise.id) ' – Stan

関連する問題