1

My complete error message is:

ActiveModel::MassAssignmentSecurity::Error in WorkoutsController#create Can't mass-assign protected attributes: workout_entry

The params that I am sending looks like:

{"workout"=>{"unit"=>"kg", "name"=>"2013-02-20T21:26:19", "note"=>nil, "workout_entry"=> [{"workout_entry_number"=>"1", "exercise_id"=>2, "entry_detail"=>[{"set_number"=>"1", "weight"=>"32", "reps"=>"43"}]}]}}

I have a workout that has many workout entries and each workout entries can have many entry details. The note is optional.

workout.rb

class Workout < ActiveRecord::Base
    has_many :workout_entries, dependent: :destroy

    attr_accessible :id, :name, :note, :unit, :workout_entries_attributes
    belongs_to :user
    accepts_nested_attributes_for :workout_entries

    validates_presence_of :name
    validates_presence_of :unit, :inclusion => %w(kg lb)
    validates_associated :workout_entries

    default_scope order("created_at DESC")

end

workout_entry.rb

class WorkoutEntry < ActiveRecord::Base

    belongs_to :workout
    belongs_to :exercise
    has_many :entry_details, dependent: :destroy

    attr_accessible :workout_id, :exercise_id, :workout_entry_number, :entry_details_attributes
    accepts_nested_attributes_for :entry_details

    validates :exercise_id, presence: true, numericality: {only_integer: true}, :inclusion => { :in => 1..790 }
    validates :workout_id, presence: true, numericality: {only_integer: true, greater_than_or_equal_to: 1}
    validates :workout_entry_number, presence: true, numericality: {only_integer: true, greater_than_or_equal_to: 1}

end

workouts_controller.rb

class WorkoutsController < ApplicationController
    respond_to :json
    before_filter :authenticate_user!

    def index
        respond_with(current_user.workouts)
    end

    def show
        respond_with(current_user.workouts.find(params[:id]))
    end

    def create
        respond_with(current_user.workouts.create(params[:workout]))
    end

    def update
        @workout = current_user.workouts.find(params[:id])
        if @workout.update_attributes(params[:workout])
          render json: @workout, status: :ok
        else
          render json: @workout.errors, status: :unprocessable_entity
        end
    end

    def destroy
        respond_with(current_user.workouts.destroy(params[:id]))
    end

end

I tried switching the ordering of attr_accessible and accepts_nested_attributes_for within the workout.rb, but it does not work.

I even tried to set

config.active_record.whitelist_attributes = true

but creating was still prevented.

4

3 回答 3

0

尝试在您的锻炼模型中可访问的 attr 中添加锻炼入口 ID。

于 2013-02-21T06:46:49.843 回答
0

我决定不在锻炼和锻炼入口模型中使用accepts_nested_attributes_for,因为它不适合我。我还更新了发送的 json 格式。详细信息在下面的链接中

关联

于 2013-02-22T07:55:16.723 回答
0

accepts_nested_attributes_for不会将任何属性添加到白名单中。无论您尝试传递给 update_attributes 的什么键都必须列在 attr_accessible 中,在您的情况下,您需要添加workout_entryattr_accessible.

看起来您的表单确实有错误,如果您使用 fields_for 那么它应该使用workout_entries_attributes您可以访问的 key 。

于 2013-02-21T06:10:46.127 回答