我收到错误<?php if(isset($modal) && $modal == true): ?>
YOUR MODAL HERE
<?php endif; ?>
在undefined local variable or method 'exercise' for #<#<Class:0x007fb8b1638be0>:0x007fb8b1627318>
页面上呈现的部分内容上调用了错误。错误在这一行:
workouts#show
以下是<%= form_for(exercise, method: "post") do |f| %>
页面上的背景信息:
workouts#show
通常,我知道这意味着变量未在控制器中定义,但定义了变量。这是<% if @workout.exercises.count == 0 %>
<p>Looks like you get a freebie for this one! No score report today. Rest up and drink some water. It ain't always that easy...</p>
<% else %>
<% @workout.exercises.each do %>
<p><%= exercise.name %></p>
<%= render 'reports/form', report: Report.new, exercise: @exercise %>
<% if current_user.admin? %>
<%= link_to "Delete", [exercise.workout, exercise], method: :delete, data: { confirm: 'Are you sure?' } %>
<% end %>
<% end %>
<h4>Your Previous Results<h4>
<% @workout.exercises.each do %>
<h5><%= exercise.name %></h5>
<%= render @workout.exercise.results %>
<% end %>
<% end %>
<% if current_user.admin? %>
<%= render 'exercises/form' %>
<% 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
@workout.user_id = current_user
end
def create
@workout = Workout.new(workout_params)
@workout.user = current_user
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
private
def workout_params
params.require(:workout).permit(:name, :workout_type, :teaser, :description, :video, :difficulty, :trainer, :user_id)
end
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 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
是:
routes.rb
我的路线本身是:
resources :workouts do
resources :exercises do
resources :reports, only: [:create, :destroy]
end
end
我希望任何人都可以分享我在这里做错的任何帮助或智慧。
答案 0 :(得分:0)
试试这个:
@exercise = workout.exercises.new
<%= form_for(@exercise,:url=>"/workouts/#{@workout.id}/exercises", method: "post") do |f| %>
当您将其声明为局部变量时,只能将其声明为实例变量而无法访问它
答案 1 :(得分:0)
如果需要在视图中获取访问权限,则需要在控制器中将变量创建为实例变量(带有@
前缀)。您实例化的练习变量只是一个本地,并且仅在您的操作中具有范围。这就是为什么你在部分中得到一个未定义的错误的原因。
编辑:另外,为了摆脱您的路径错误,请将url: workout_exercises_path
添加到您的form_for,因为您使用嵌套资源进行锻炼练习组合
答案 2 :(得分:0)
您需要在控制器中声明@exercise
变量。
在锻炼控制器中添加:
def show
@workout = Workout.find(params[:id])
@exercise = Exercise.new
end
这在你看来:
<%= form_for([@workout, @exercise]) do |f| %>
...
<% end %>
[@workout, @exercise]
是必要的,因为您使用的是嵌套表单。