2013-07-28 2 views
0

첫 번째 응용 프로그램에서 작업 중이며 조금 갇혀 있습니다. 중첩 된 특성에 대한 개정 된 레일 스 캐스트를 따르고 있지만 양식에 필드가 표시되지 않습니다. 아래 내용은 제가 가지고있는 것입니다. 날짜와 제출 필드가 모두 표시되지만 운동을 위해 노력하고있는 필드 (그리고 담당자와 체중에 필요한 필드)는 전혀 표시되지 않습니다. 마치 렌더링 될 때 존재하지 않는 것처럼 보입니다.레일에 중첩 된 속성보기에 문제가 있습니다.

/views/workouts/_workout_form.html.erb 읽기 :

<%= form_for(@workout) do |f| %> 
    <%= render 'common/form_errors', object: @workout %> 

    <p> 
     <%= date_select :workout, :workout_date %><br /> 

     <%= f.fields_for :exercises do |builder| %> 
     <fieldset> 
      <%= builder.label :movement, "Movement" %><br /> 
      <%= builder.text_area :movement %> 
     </fieldset> 
     <% end %> 

    </p> 

    <p> 
     <%= f.submit "Log It" %> 
    </p> 


<% end %> 

/views/workouts/index.html.erb 읽기 :

<%= provide(:title, 'GymLog') %> 

<div id='ask'> 
<h1>Post a Workout</h1> 
<% if logged_in? %> 
<%= render 'workout_form' %> 
<% else %> 
<p>Please login</p> 
<% end %> 
</div> 

/모델/exercise.rb 읽습니다 :

class Exercise < ActiveRecord::Base 
    belongs_to :workout 
    attr_accessible :movement, :reps, :weight 
end 
,

/models/workout.rb

class Workout < ActiveRecord::Base 
    belongs_to :user 
    has_many :exercises 
    attr_accessible :workout_date, :exercises_attributes 
    accepts_nested_attributes_for :exercises 
end 

/controllers/workout_controller.rb

class WorkoutsController < ApplicationController 
    before_filter :auth, only: [:create] 

    def index 
    @workout = Workout.new 
    end 

    def address_attributes=(attributes) 
    end 

    def create 
    @workout = current_user.workouts.build(params[:workout]) 
    if @workout.save 
     flash[:success] = 'Workout Recorded' 
     redirect_to root_url 
    else 
     render 'index' 
    end 
    end 
end 

/controllers/exercises_controller.rb

class ExercisesController < ApplicationController 
    def new 
    end 
end 
+0

컨트롤러 작업에서 @workout 개체에 대한 연습을 빌드합니까? –

+0

@NickKugaevsky railscast는 컨트롤러를 넘기지 않으므로 아마 그렇지 않을 것입니다. 컨트롤러를 포함하도록 게시물 편집 – Ryan

답변

0

너 컨트롤러 액션에서 새롭게 초기화 된 객체를위한 중첩 된 객체를 빌드해야합니다. 사용해보기 :

# controllers/workouts_controller.rb 

class WorkoutsController < ApplicationController 
    # ... 

    def index 
    @workout = Workout.new 
    @workout.exercises.build 
    end 

    # ... 
end 
+0

감사합니다! 그랬어. – Ryan