2016-06-01 96 views
1

我看過很多類似的帖子,但似乎無法擺脫這個record not found錯誤,當我嘗試使用我的destroy方法時得到的錯誤。有問題的兩個型號是workouts.rbexercises.rb。鍛鍊has_many練習。未找到軌道記錄錯誤未找到身份證找不到鍛鍊

我得到的錯誤是Couldn't find Workout without an ID從我exercises_controller第三線以下代碼:

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 

編輯:

調用刪除代碼是:

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

哪裏這個錯誤是來自任何想法?

+0

什麼是'參數'來到'摧毀'行動? – Pavan

+0

你能粘貼代碼,從哪個請求都來這裏 – Mukesh

+0

@Mukesh,我加入了僱員再培訓局的代碼我原來的職位。 – Liz

回答

1

如果沒有代碼,我們不能爲您做很多事情。但是,常見的錯誤是忘記路線上的:id

是看你使用Rails自動生成它們,所以很容易忘記,你exercice資源破壞的行動將是對/exercises/:id

心靈的路線id一個DELETE行動!

而且,讀你的控制器,它看起來像你期望workout_id,那麼正確的路由是:

resources :workouts do 
    resources :exercices 
end 

這是要去使有這些路線:

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 

注意嵌套路線中的workout_id

所以,如果你有鍛鍊與id = 3,爲exercice與id = 23,你就可以在/workouts/3/exercices/23,並在您ExercicesController發送DELETE,你將有機會獲得這兩個值:

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

就像你期望的那樣,我想。

我過了一會兒,希望它有用。我試圖根據您的代碼豐富我的答案。

+0

我增強我原來的職位與再培訓局的代碼。對不起,這樣一個明顯的疏忽。 – Liz

+0

我確實原本有嵌套的路線是這樣的,但我最終取消嵌套他們,因爲這是造成更多的問題比它解決。 – Liz

+0

無論哪種方式,你的刪除按鈕,你的路,你需要指定'id',就像這樣:'exercise_path(ID:exercise.id)' – Stan