我正在创建一个应用程序,允许用户记录锻炼和锻炼,锻炼作为我锻炼表单中的嵌套模型。
路线.rb:
resources :users do
resources :workouts
end
锻炼.rb:
attr_accessible :name, :exercises_attributes
has_many :exercises
belongs_to :user
accepts_nested_attributes_for :exercises, :allow_destroy => true
练习.rb:
attr_accessible :name, :weight, :reps
belongs_to :workout
在我的锻炼控制器.rb 中:
def create
@workout = @user.workouts.new(workout_params)
@exercise = @workout.exercises.new(params[:workout][:exercise])
respond_to do |format|
if @workout.save && @exercise.save
//...etc
在我的锻炼/_form.html.erb 中:
<%= form_for [@user, @workout], builder: LabeledFormBuilder do |f| %>
<%= f.text_field :name %>
<%= f.fields_for :exercises do |builder| %>
<%= builder.text_field :name %>
<%= builder.text_field :weight %>
<%= builder.text_field :reps %>
<% end %>
<%= f.submit %>
<% end %>
我无法让 fields_for 按照文档(http://edgeapi.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for)中的规定工作。
它说如果它处于一对多关联中,您应该能够将模型的复数版本传递给 fields_。这在我的锻炼#new 动作中很好用,但在我的锻炼#edit 中,我必须传入单数版本 (:exercise) 才能显示对象(锻炼)的当前属性值。文档说:
通常这可以通过仅将模型对象的名称传递给 fields_for 来简化 - ...在这种情况下,如果 :permission 也恰好是实例变量 @permission 的名称,则输入字段的初始状态将反映该变量的属性@permission.admin。
将 :exercises 传递给 fields_for 不会将属性保存到数据库,而将 :exercise 传递给 fields_for 会将其保存到数据库,但不会在编辑操作中显示当前属性值。
在锻炼#new 和 #edit: 中使用 fields_for :exercise
<div class="field"><input id="workout_exercise_name" name="workout[exercise][name]" placeholder="name" type="text" /></div>
与 fields_for :练习
<div class="field"><input id="workout_exercises_attributes_0_name" name="workout[exercises_attributes][0][name]" placeholder="name" type="text" /></div>
最后,我在锻炼#create 的开发日志中有以下内容:
Started POST "/users/5/workouts" for 127.0.0.1 at 2013-04-07 14:13:45 -0400
Processing by WorkoutsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"a98ozlUWDtZc49v/PfFds/V0S56R2Y73HsxW6+wjYWI=", "workout"=>{"name"=>"test", "exercise"=>{"name"=>"lunge", "weight"=>"30", "reps"=>"20", "_destroy"=>""}}, "commit"=>"Create Workout", "user_id"=>"5"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", "5"]]
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."auth_token" = 'dHhQQSEecDOetjh-ZROpmw' LIMIT 1
Unpermitted parameters: exercise
WARNING: Can't mass-assign protected attributes for Exercise: _destroy
app/controllers/workouts_controller.rb:32:in `create'
(0.2ms) BEGIN
SQL (0.8ms) INSERT INTO "workouts" ("created_at", "name", "updated_at", "user_id") VALUES ($1, $2, $3, $4) RETURNING "id" [["created_at", Sun, 07 Apr 2013 18:13:45 UTC +00:00], ["name", "test"], ["updated_at", Sun, 07 Apr 2013 18:13:45 UTC +00:00], ["user_id", 5]]
SQL (0.6ms) INSERT INTO "exercises" ("created_at", "name", "reps", "updated_at", "weight", "workout_id") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["created_at", Sun, 07 Apr 2013 18:13:45 UTC +00:00], ["name", "lunge"], ["reps", 20], ["updated_at", Sun, 07 Apr 2013 18:13:45 UTC +00:00], ["weight", 30], ["workout_id", 64]]
(0.6ms) COMMIT
(0.4ms) BEGIN
(0.2ms) COMMIT
Redirected to http://localhost:3000/users/5/workouts
Completed 302 Found in 21ms (ActiveRecord: 3.5ms)
注意不允许的参数:exercise。
有谁知道为什么会这样?我超级难过。任何帮助都会非常棒。如果没有人有任何想法,我可能会尝试创建一个新的 rails 4 应用程序并测试这些关联。