我的应用程序具有一些公认的嵌套路线。每次锻炼has_many
锻炼,每次锻炼has_many
。在尝试创建一个新练习时,我收到一个错误:
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 :(得分:1)
您的练习嵌套在锻炼资源下。 因此,要创建新练习,您需要指定您尝试创建练习的练习。
如路线所示,您需要点击
[POST] /workouts/:workout_id/exercises/new
这条路线用于创建练习。
而不是错误[POST] "/exercises/new"
中显示的内容。
您对路线的设计意味着并要求您为每项运动指定其属于哪种运动。每个报告都必须指明哪个练习以及由此属于哪个练习。
如果您的要求不同,您可以更改设计。