2016-06-01 42 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