在使用Ruby 2.3,Rails 5.0,Windows(32位)时使用accepts_nested_attributes_for方法时,无法在视图中看到嵌套字段

时间:2016-12-07 14:49:40

标签: ruby-on-rails ruby

我正在尝试构建一个锻炼应用程序,您可以在其中提交包含多个轨道练习的锻炼。我试图创造一个新的"锻炼视图,你可以提交一个新的锻炼与嵌套练习,但我的"新"视图仅显示锻炼表单字段,但不显示锻炼表单字段。顺便说一句,我使用的是Ruby 2.3和Rails 5.0。谁能看到我做错了什么?

锻炼模型(workout.rb)

class Workout < ActiveRecord::Base
    has_many :exercises, :dependent => :destroy
    accepts_nested_attributes_for :exercises
end

练习模型(exercise.rb)

class Exercise < ActiveRecord::Base
    belongs_to :workout
end

锻炼控制器(workouts_controller.rb)

class WorkoutsController < ApplicationController
    def new
        @workout = Workout.new
        @workout.exercises.build
    end
end

新锻炼视图(views \ workouts \ new.html.erb)

<h1>Create New Workout</h1>

<%= form_for(@workout) do |f| %>
  <%= f.number_field :workout_length, :placeholder => "Workout length   (minutes)" %> <br>
  <%= f.text_field :workout_description, :placeholder => "Workout description" %> <br>

<% f.fields_for :exercises do |builder| %>
<p>
    <%= builder.text_field :exercise_description %>
</p>
<% end %>
  <%= f.submit "SUBMIT WORKOUT" %>

架构(schema.rb)

ActiveRecord::Schema.define(version: 20161207040053) do
  create_table "exercises", force: :cascade do |t|
    t.string   "exercise_description"
    t.integer  "workout_id"
    t.datetime "created_at",           null: false
    t.datetime "updated_at",           null: false
    t.index ["workout_id"], name: "index_exercises_on_workout_id"
  end

  create_table "workouts", force: :cascade do |t|
    t.integer  "workout_length"
    t.string   "workout_description"
    t.datetime "created_at",          null: false
    t.datetime "updated_at",          null: false
  end
end

1 个答案:

答案 0 :(得分:0)

我明白了......我没有在运动和锻炼模型之间建立正确的关系。