2016-05-22 12 views
1

Ich erhalte den Fehler undefined local variable or method 'exercise' for #<#<Class:0x007fb8b1638be0>:0x007fb8b1627318>undefiniert lokale Variable oder eine Methode, noch Variable in der Steuerung definiert

Der Fehler auf einem Teil auf meiner workouts#show Seite gerendert aufgerufen wird. Der Fehler in dieser Zeile ist:

<%= form_for(exercise, method: "post") do |f| %> 

Und hier ist der Zusammenhang auf der workouts#show Seite:

<% 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 %> 

Normalerweise Ich weiß, das bedeutet, dass der Variable nicht in der Steuerung definiert ist, aber die Variable definiert. Hier ist die 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 

Und die 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 

Ich habe auch Probleme mit Routing wurde mit (diese Routen sind verschachtelt), so dass kann oder auch nicht ein Faktor sein kann:

routes.rb ist:

resources :workouts do 
    resources :exercises do 
     resources :reports, only: [:create, :destroy] 
    end 
    end 

Und mein r OUTES selbst sind:

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 

Ich würde jede mögliche Hilfe oder Weisheit Liebe jemand auf alles teilen kann ich hier falsch mache.

Antwort

0

Sie müssen verschachtelt verwenden, um die @exercise Variable in Ihrem Controller erklären. Fügen Sie diese in Ihrem Training Controller:

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

Und dies aus Ihrer Sicht:

<%= form_for([@workout, @exercise]) do |f| %> 
    ... 
<% end %> 

Die [@workout, @exercise] notwendig ist, da Sie eine verschachtelte Form verwenden.

+0

Dies funktioniert für die Variablen. Dieses Routing erzeugt jedoch den Fehler 'No route matches [POST] "/ workouts/workout_exercise_path" '. – Liz

+0

Ich habe meinen Beitrag bearbeitet, entfernen Sie die Methode:: post –

+0

Es sagt immer noch 'Keine Route entspricht [POST]"/workouts/workout_exercise_path "'. – Liz

0

versuchen Sie dies:

@exercise = workout.exercises.new 
<%= form_for(@exercise,:url=>"/workouts/#{@workout.id}/exercises", method: "post") do |f| %> 

Wie Sie es als eine lokale Variable declar ist es nicht Seite zugreifbar heraus, dass declar es nur als eine Instanz-Variable

+0

Dies mindestens den Fehler geändert, um das erste Argument in Form kann nicht enthalten oder leer sein nil, in der gleichen Zeile aufgerufen. – Liz

+0

fügen Sie den Formularteil zu –

+0

hinzu Es scheint zu sein, sich fortzuentwickeln! Nun lautet der Fehler 'undefinierte Methode' exercises_path 'für # <# : 0x007fb8b9a281a8> '. – Liz

0

Sie müssen die Variable als Instanzvariable erstellen (mit einem Präfix @) in Ihrem Controller, wenn Sie Zugriff in Ihrer Ansicht benötigen. Die Übungsvariable, die Sie instanziieren, ist nur eine lokale Variable und hat nur innerhalb Ihrer Aktion einen Gültigkeitsbereich. Das ist der Grund, warum Sie in Ihrem Partial einen undefinierten Fehler bekommen.

Edit: auch, um loszuwerden, Ihre Wegfehlers, fügt url: workout_exercises_path zu Ihrem form_for becasue Sie Ressourcen für Ihre Training-Übungen Combo

Verwandte Themen