Attr_accessor does not work Rails 4

I have a problem with my code. I think this code is correct, but I do not know what will happen? Here is my code:

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

My controller

  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

The problem is that when I put the data in :notesand :place, the check is not yet performed. Please help me guys. Thank!

+4
source share
2 answers

The problem is that when I put the data in: notes and: place, it returns an error even I have data input. Please help me guys.

attr_accessor :notes :place. , attr_accessor:

attr_accessor :create_registration, :is_contact_registration, :is_appointment_registration, :notes, :place
+2

Rails 4 attr_accessor ( ) , . , attr_accessor , :

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

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

@Rich , , , .

: http://edgeapi.rubyonrails.org/classes/ActionController/StrongParameters.html

0

Source: https://habr.com/ru/post/1542430/


All Articles