Rails的4.0测试版,fields_for不接受多元化的模型名称在一个一对多的关联 [英] Rails 4.0 beta, fields_for not accepting pluralized model_name in one-to-many association

查看:130
本文介绍了Rails的4.0测试版,fields_for不接受多元化的模型名称在一个一对多的关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个应用程序,允许用户记录训练和演习,以练习为我的训练表中嵌套的模式。

I am creating an app to allow users to record workouts and exercises, with the exercises as a nested model within my workouts form.

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

在我的 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

在我的试训/ _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).

I am having trouble getting fields_for to work as stipulated in the documentation ( http://edgeapi.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for).

它说,你应该能够传递给fields_for模型的多元化的版本,如果它是一个一对多的关联。这在我的训练工作正常#新的动作,但在我的训练#编辑,我在singularized版本传递(:运动),以显示对象(锻炼)的当前属性值。该文件说:

It says that you should be able to pass to fields_for the pluralized version of your model if it is in a one-to-many association. This works fine in my workouts#new action, but in my workouts#edit, I have to pass in the singularized version (:exercise) in order to display the current attribute values of the object(the workout). The docs say:

通常,这可以通过传递模型的只是名称被简化
  对象fields_for - ...在这种情况下,如果:许可也恰好
  是一个实例变量@permission的名称,的初始状态
  输入字段将反映该变量的属性的值
  @ permission.admin。

Often this can be simplified by passing just the name of the model object to fields_for - …in which case, if :permission also happens to be the name of an instance variable @permission, the initial state of the input field will reflect the value of that variable’s attribute @permission.admin.

传:练习fields_for不保存属性到数据库中,而经过:锻炼fields_for做它保存到数据库中,但在编辑操作不显示当前的属性值

passing :exercises to fields_for does not save the attributes to the database, whereas passing :exercise to fields_for does save it to the database, but doesn't display the current attribute values in the edit action.

在训练#新#edit:
与fields_for:运动

In workouts#new and #edit: with 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>

最后,我已经中我的开发日志上锻炼#创建如下:

Finally I have the following in my dev log on workout#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)

通知不允许的参数:运动

有谁知道为什么发生这种情况?我是超级难住了。任何帮助将是超真棒。我可能会尝试,并创建一个新的Rails应用程序4和测试这些协会在那里,如果没有人有任何想法。

Does anyone know why this is happening? I am super stumped. Any help would be super awesome. I might try and create a new rails 4 app and test these associations out there if no one has any ideas.

推荐答案

在一个轨道4的应用程序,你需要切换到使用强参数的质量分配策略。这意味着消除从模型attr_accessible,并放置在您的控制器。这就是你的日志告诉你的线6-7。

In a rails 4 app you'll need to switch over your mass assignment strategy to use strong parameters. It means removing attr_accessible from your models and placing them in your controller. That's what your log is telling you on lines 6-7.

使用嵌套的属性在控制器的workout_params会想看看这样的事情:

With nested attributes the workout_params in your controller will want to look something like this:

private

def workout_params
  params.require(:workout).permit(
    :id,
    :next_attribute,
    :next_attribute,
    exercises_attributes: [
    :id,
    :next_attribute,
    :next_attribute
  ])
end

有各种各样的资源在那里为强大的参数的详细信息 - 这里有一个由瑞恩·贝茨:的 http://railscasts.com/episodes/371-strong-parameters

There are a variety of resources out there for more information on strong parameters - here's one from Ryan Bates: http://railscasts.com/episodes/371-strong-parameters

这篇关于Rails的4.0测试版,fields_for不接受多元化的模型名称在一个一对多的关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆