How is the server supposed to remove the fields in the action update?



  • Letsume I have an app questionnaire, poll has many questions, they have many answers, all connected through. accepts_nested_attributes_for

    The client interacts with the server through json-api, fills in all fields, presses for save, the data goes to create

      def create
        @poll = current_user.polls.create(poll_params)
    
    if @poll.persisted?
      render 'show', status: :created
    else
      render json: { errors: @poll.errors }, status: :internal_server_error
    end
    

    end

    private
    def poll_params
    params.require(:poll).permit(
    :title,
    # has many
    {
    questions: [
    :id,
    :content,
    {
    # has many
    :options => [:id, :content]
    }
    ]
    }
    )
    end

    The question is what to do if it's on the page. editthe client decides to remove some fields? New data retained update (@poll.update_attributes(poll_params)and there should I remove all poll investments except the poll itself and re-establish them? or should there be a special field in the request? deleted_attr with the type and id of all remote fields?
    Data should be sent on a single request, because the client will, in the formatting, cancel the field.

    Models

    class Poll < ActiveRecord::Base
    belongs_to :user
    has_many :questions, :dependent => :destroy
    accepts_nested_attributes_for :questions, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true

    alias

    def questions=(value)
    value.is_a?(Array) && value.first.is_a?(Hash) ? (self.questions_attributes = value) : super
    end
    end

    class Question < ActiveRecord::Base
    belongs_to :poll
    has_many :answers, :dependent => :destroy
    accepts_nested_attributes_for :answers, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true

    alias

    def answers=(value)
    value.is_a?(Array) && value.first.is_a?(Hash) ? (self.answers_attributes = value) : super
    end
    end

    class Answer < ActiveRecord::Base
    belongs_to :question
    acts_as_voteable # thumbs_up gem
    end



  • According to the http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html ♪ Disposal The recordings shall be made by reference in key parameters _destroy with "right" meaning. false or nil)

    When it's on the record, the system, if retained, will ensure that it is no longer in the database or that it is not retained (if it was not there) or removed (if it was).


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2