4

このコードは正しいと思いますが、何が起こるかわかりませんか? これが私のコードです:

registration.rb :

class Registration < ActiveRecord::Base
  attr_accessor :create_registration, :is_contact_registration, :is_appointment_registration

  validates :client_id, presence: true
  validates :type, presence: true
  validates :contact_thru, presence: true
  validates :purpose_message, presence: true,   :unless => :is_appointment_registration
  validates :action_needed, presence: true,     :unless => :is_appointment_registration
  validates :date_created, presence: true
  validates :owner_id, presence: true
  validates :status, presence: true
  validates :notes, presence: true  

end

私のコントローラー、

  def create
    binding.pry
    @registration = Registration.new(record_params)
    @registration.owner_id = current_user.id

    @registration.is_appointment_registration = true
    if @registration.save
      render json: @registration, status: :created, location: @registration
    else
      render json: @registration.errors, status: :unprocessable_entity
    end

  end

問題は、データを と に配置する:notes:place、検証がまだ失敗することです。

4

2 に答える 2

1

Rails 4 では、attr_accessor (モデル上にあります) が廃止され、コントローラーに含まれる強力なパラメーターが採用されました。次のように、コードを更新して、コントローラーのすべてのフィールドを attr_accessor から params に移動する必要があります。

def create
    ModelName.new(controller_name_params)
    ...
end

private
def controller_name_params
        params.require(:controller_name).permit(
            :field1,
            :field2
        )
    end

@Rich が言うように、ユーザーが変更できるようにするすべてのフィールドを params に追加してください。

ドキュメントの詳細については、こちら: http://edgeapi.rubyonrails.org/classes/ActionController/StrongParameters.html

于 2014-05-29T21:30:06.970 に答える