2016-05-22 93 views
1

我的應用使用了一些固定嵌套的路由。每次鍛鍊has_many習題,並且每次練習has_many結果。在試圖創建一個新的練習,我發現了一個錯誤:Rails沒有路由匹配[POST]「/ Exercises/new」

No route matches [POST] "/exercises/new" 

這是我workouts#show頁面上的部分,所有的魔法發生:

<%= render 'exercises/form', exercise: Exercise.new, workout: @workout %> 

這裏是exercises/_form.html.erb部分:

<%= form_for [workout, exercise] do |f| %> 

...(form stuff)... 

<div class="text-center"><%= f.submit "Create Exercise", class: 'btn btn-primary' %></div> 

<% end %> 

這裏是我的exercises#controller

class ExercisesController < ApplicationController 
    before_action :require_sign_in 
    before_action :authorize_user, only: [:destroy, :create] 

    def index 
    @exercises = Exercise.all 
    end 

    def new 
    @exercise = Exercise.new 
    end 

    def create 
    @workout = Workout.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.find(params[:post_id]) 
    exercise = @workout.exercise.find(params[:id]) 

    if comment.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, :seconds, :weight, :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 

這裏是我的路線:

workout_exercise_reports POST /workouts/:workout_id/exercises/:exercise_id/reports(.:format)  reports#create 
workout_exercise_report DELETE /workouts/:workout_id/exercises/:exercise_id/reports/:id(.:format) reports#destroy 
     workout_exercises GET /workouts/:workout_id/exercises(.:format)       exercises#index 
         POST /workouts/:workout_id/exercises(.:format)       exercises#create 
    new_workout_exercise GET /workouts/:workout_id/exercises/new(.:format)      exercises#new 
    edit_workout_exercise GET /workouts/:workout_id/exercises/:id/edit(.:format)     exercises#edit 
     workout_exercise GET /workouts/:workout_id/exercises/:id(.:format)      exercises#show 
         PATCH /workouts/:workout_id/exercises/:id(.:format)      exercises#update 
         PUT /workouts/:workout_id/exercises/:id(.:format)      exercises#update 
         DELETE /workouts/:workout_id/exercises/:id(.:format)      exercises#destroy 
       workouts GET /workouts(.:format)            workouts#index 
         POST /workouts(.:format)            workouts#create 
      new_workout GET /workouts/new(.:format)           workouts#new 
      edit_workout GET /workouts/:id/edit(.:format)          workouts#edit 
       workout GET /workouts/:id(.:format)           workouts#show 
         PATCH /workouts/:id(.:format)           workouts#update 
         PUT /workouts/:id(.:format)           workouts#update 
         DELETE /workouts/:id(.:format)           workouts#destroy 

這來源於此routes.rb結構:

resources :workouts do 
    resources :exercises do 
     resources :reports 
    end 
    end 

我歡迎任何智慧的人可以擺脫的情況,包括如何修正這個錯誤(主要),但如果有更好的方式來構建我的路線。

附加信息

這裏是我的workouts#controller

class WorkoutsController < ApplicationController 
    def index 
    @workouts = Workout.all 
    end 

    def show 
    @workout = Workout.find(params[:id]) 
    workout = @workout 
    exercise = workout.exercises.new 
    end 

    def new 
    @workout = Workout.new 
    end 

    def create 
    @workout = Workout.new 
    @workout.name = params[:workout][:name] 
    @workout.workout_type = params[:workout][:workout_type] 
    @workout.teaser = params[:workout][:teaser] 
    @workout.description = params[:workout][:description] 
    @workout.video = params[:workout][:video] 
    @workout.difficulty = params[:workout][:difficulty] 
    @workout.trainer = params[:workout][:trainer] 
    @workout.user_id = params[:workout][:user_id] 

    if @workout.save 
     flash[:notice] = "Workout was saved successfully." 
     redirect_to @workout 
    else 
     flash.now[:alert] = "Error creating workout. Please try again." 
     render :new 
    end 
    end 

    def edit 
    @workout = Workout.find(params[:id]) 
    end 

    def update 
    @workout = Workout.find(params[:id]) 

    @workout.name = params[:workout][:name] 
    @workout.workout_type = params[:workout][:workout_type] 
    @workout.teaser = params[:workout][:teaser] 
    @workout.description = params[:workout][:description] 
    @workout.video = params[:workout][:video] 
    @workout.difficulty = params[:workout][:difficulty] 
    @workout.trainer = params[:workout][:trainer] 
    @workout.user_id = params[:workout][:user_id] 

    if @workout.save 
     flash[:notice] = "Workout was updated successfully." 
     redirect_to @workout 
    else 
     flash.now[:alert] = "Error saving workout. Please try again." 
     render :edit 
    end 
    end 

    def destroy 
    @workout = Workout.find(params[:id]) 

    if @workout.destroy 
     flash[:notice] = "\"#{@workout.name}\" was deleted successfully." 
     redirect_to action: :index 
    else 
     flash.now[:alert] = "There was an error deleting the workout." 
     render :show 
    end 
    end 
end 

這裏是錯誤的全部跟蹤:

actionpack (4.2.5) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call' 
actionpack (4.2.5) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call' 
railties (4.2.5) lib/rails/rack/logger.rb:38:in `call_app' 
railties (4.2.5) lib/rails/rack/logger.rb:20:in `block in call' 
activesupport (4.2.5) lib/active_support/tagged_logging.rb:68:in `block in tagged' 
activesupport (4.2.5) lib/active_support/tagged_logging.rb:26:in `tagged' 
activesupport (4.2.5) lib/active_support/tagged_logging.rb:68:in `tagged' 
railties (4.2.5) lib/rails/rack/logger.rb:20:in `call' 
actionpack (4.2.5) lib/action_dispatch/middleware/request_id.rb:21:in `call' 
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call' 
rack (1.6.4) lib/rack/runtime.rb:18:in `call' 
activesupport (4.2.5) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call' 
rack (1.6.4) lib/rack/lock.rb:17:in `call' 
actionpack (4.2.5) lib/action_dispatch/middleware/static.rb:116:in `call' 
rack (1.6.4) lib/rack/sendfile.rb:113:in `call' 
railties (4.2.5) lib/rails/engine.rb:518:in `call' 
railties (4.2.5) lib/rails/application.rb:165:in `call' 
rack (1.6.4) lib/rack/lock.rb:17:in `call' 
rack (1.6.4) lib/rack/content_length.rb:15:in `call' 
rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service' 
/Users/elizabethbayardelle/.rbenv/versions/2.2.4/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service' 
/Users/elizabethbayardelle/.rbenv/versions/2.2.4/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run' 
/Users/elizabethbayardelle/.rbenv/versions/2.2.4/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread' 
+0

它顯示錯誤的哪一行? – uday

+0

沒有線,它只是顯示我的路線... – Liz

+0

在你的應用程序中沒有路線'[POST]「/ Exercises/new」'。你的路線中有'[GET]'。 –

回答

1

你必須在你的鍛鍊資源嵌套演習。 因此,要創建一個新的練習,您需要指定您正在嘗試創建練習的鍛鍊。

如路線所示,您需要點擊 [POST] /workouts/:workout_id/exercises/new 此路線才能創建練習。 而不是在錯誤[POST] "/exercises/new"中顯示的內容。

您的路線設計意味着並要求您爲每項鍛鍊指定它屬於哪種鍛鍊。並且每份報告都必須指定哪項練習以及哪項練習屬於哪一項。

如果您的要求不同,您可以更改設計。

+0

我試圖取消它們的嵌套,但我仍然收到相同的錯誤消息。 – Liz

+0

您已將'exercise/new'定義爲GET –

相關問題