检查更新操作中添加的新嵌套属性 rails 4

check new nested attribute added in update action rails 4

我有嵌套形式,其中 Passenger 和 passenger_cards 分别是 Parent 和 child

我可以add/remove我想要的形式的乘客卡(部分支持railscast 197),工作得很好。

但是当编辑操作时,我怎么知道编辑页面上添加了新卡片。

我的创作动作

 @admin_passenger = Passenger.new(admin_passenger_params)
i=0
respond_to do |format|
  if  @admin_passenger.valid?
  params[:passenger][:passenger_cards_attributes].values.each do |p|
    i += 1
    @admin_passenger.card_number = p[:card_number]
    @admin_passenger.card_expiry_month = p[:card_expiry_month]
    @admin_passenger.card_expiry_year = p[:card_expiry_year]
    @admin_passenger.card_cvc = p[:card_cvc]
    if i==1   
      @errors,@error_message = @admin_passenger.create_customer_payment_profile
    else
      @errors,@error_message = @admin_passenger.add_another_card   
    end
    if @errors == true
      flash.now[:alert] = @error_message
      format.html { render :new }
    else
      @admin_passenger.save
      @admin_passenger.get_paymentprofile(@admin_passenger.profile_id)
      passenger_card = @admin_passenger.passenger_cards.build(payment_profile_id: @admin_passenger.payment_profile_id, card_last_4: @admin_passenger.card_last_4, is_active: true, card_exp_month: @admin_passenger.card_expiry_month, card_exp_year: @admin_passenger.card_expiry_year, card_cvc: @admin_passenger.card_cvc)
      passenger_card.save
    end  
      format.html { redirect_to admin_passenger_path(@admin_passenger), notice: 'Passenger was successfully created.' }
      format.json { render :show, status: :created, location: @admin_passenger }
    end
  else
    #flash.now[:alert] = @admin_passenger.check_valid_card_details ?  @admin_passenger.errors.full_messages : @admin_passenger.errors.full_messages << "Invalid Card Details" 
    #@admin_passenger.errors.add(:card_number, "Invalid Card Details")
    format.html { render :new }
    format.json { render json: @admin_passenger.errors, status: :unprocessable_entity }
  end
end

我的更新操作

respond_to do |format|
  params[:passenger][:action] = params[:passenger][:action].to_i
  if @admin_passenger.update(update_passenger_params)
    logger.warn("=====55===#{@admin_passenger.passenger_cards.inspect }====")
    format.html { redirect_to admin_passenger_path(@admin_passenger), notice: 'Passenger was successfully updated.' }
    format.json { render :show, status: :ok, location: @admin_passenger }
  else
    format.html { render :edit }
    format.json { render json: @admin_passenger.errors, status: :unprocessable_entity }
  end
end

我需要在添加卡上做一些处理,所以

def add_card
@admin_passenger = Passenger.find(params[:id])
if params[:passenger].present?
  @admin_passenger.card_number = params[:passenger][:card_number]
  @admin_passenger.card_expiry_year = params[:passenger][:card_expiry_year]
  @admin_passenger.card_expiry_month = params[:passenger][:card_expiry_month]
  @admin_passenger.card_cvc = params[:passenger][:card_cvc]
  puts("===================******")
  logger.warn("passenger==> #{@admin_passenger.card_number.inspect}===year #{@admin_passenger.card_expiry_year.inspect}===month #{@admin_passenger.card_expiry_month.inspect}")
  status = @admin_passenger.check_valid_card_details
  if !status
    flash.now[:alert] = "Invalid Card Details"       
  else
    @errors,@error_message = @admin_passenger.add_another_card
    if @errors == true
      flash.now[:alert] = @error_message
    else          
      redirect_to admin_passenger_path(@admin_passenger), notice: 'Passenger card was successfully added.' 
    end
  end
end

结束

my question is if any way to know that a new card is added on edit form so i can call add_card method and further process that card

谢谢

其实你不需要这样做,如果你的关系设置正确并且你的表格没问题,你可以一次保存所有卡片。只是做:

@admin_passenger = Passenger.update(passenger_params)

然后将passenger_params定义为私有方法:

  private
   def passenger_params
     #complete the array below to contain all the required fields
     params.require(:passenger).permit(:id, :passenger_cards_attributes => [:card_number, :card_expiry])
   end

对于您的嵌套属性,您将获得之前添加的乘客卡的 Id。请检查您的控制台日志以了解嵌套属性参数。

在passenger_cards_attributes =>{"0"=>{"id"=>"105","card_number" => "123"},"1231232444" => {"card_number" => "456"}}.

此处 card_number "123" 的参数已经存在,因此将使用 "id" 和 card_number "456" 传递" 这是您新创建的,因此参数中没有 "id"。因此,只需为那些没有 id 的属性调用 "add_card" 方法。