2016-05-22 88 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 –

相关问题