我正在创建一个应用程序,允许用户记录锻炼和锻炼,锻炼作为一个嵌套模型在我的锻炼表单。
routes.rb:
resources :users do
resources :workouts
end
workouts.rb:
attr_accessible :name, :exercises_attributes
has_many :exercises
belongs_to :user
accepts_nested_attributes_for :exercises, :allow_destroy => true
exercise.rb:
attr_accessible :name, :weight, :reps
belongs_to :workout
在my workouts_controller.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
我在<<p> strong>训练/_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_。这在我的workout #new action中工作得很好,但是在我的workout #edit中,为了显示对象(锻炼)的当前属性值,我必须传入单数版本(:exercise)。医生说:
通常,这可以通过只传递模型的名称来简化对象到fields_for -…在这种情况下,if:permission也发生在是实例变量@permission的名称,是的初始状态输入字段将反映该变量的属性值@permission.admin。
将:exercises传递给fields_for不会将属性保存到数据库,而将:exercise传递给fields_for会将其保存到数据库,但不会在编辑操作中显示当前属性值。
在训练#new和#edit中:with fields_for:exercise
<div class="field"><input id="workout_exercise_name" name="workout[exercise][name]" placeholder="name" type="text" /></div>
with fields_for:exercises
<div class="field"><input id="workout_exercises_attributes_0_name" name="workout[exercises_attributes][0][name]" placeholder="name" type="text" /></div>
最后,我在我的开发日志中有以下内容:
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)
notice 不允许的参数:exercise.
有人知道为什么会这样吗?我被难住了。任何帮助都会非常棒。如果没有人有任何想法,我可能会尝试创建一个新的rails 4应用程序并测试这些关联。
在rails 4应用程序中,你需要切换你的质量分配策略来使用强参数。这意味着从模型中删除attr_accessible并将它们放在控制器中。这是日志在第6-7行告诉你的。
使用嵌套属性,控制器中的workout_params将看起来像这样:
private
def workout_params
params.require(:workout).permit(
:id,
:next_attribute,
:next_attribute,
exercises_attributes: [
:id,
:next_attribute,
:next_attribute
])
end
关于强参数的更多信息有各种各样的资源,这里有一个来自Ryan Bates: http://railscasts.com/episodes/371-strong-parameters