簡體   English   中英

update_all不更新我的數據庫

[英]update_all not updating my database

我的預訂數據庫和汽車數據庫之間存在多對多關系,以下內容位於我的預訂控制器中,並路由到該職位。

def reserveConfirm

pick_time = params[:pick_y].to_s+"-"+params[:pick_m].to_s+"-"+params[:pick_d].to_s+" "+"#{params[:pick_h]}:00:00"
return_time = params[:return_y].to_s+"-"+params[:return_m].to_s+"-"+params[:return_d].to_s+" "+"#{params[:return_h]}:00:00"
#@reservation = Reservation.find(params[:id])
@car = Car.where(:id => params[:car_id]).update(:status => 'reserved')
@reservation = Reservation.new(status:'reserved',
                               pick_up_time: pick_time,
                               return_time: return_time,
                               user_id:current_user.id)



  if  @reservation.save
    #flash[:success] = @reservation.id
    flash[:success] = 'shit'
    redirect_to(:action => 'history',:notice => 'Check out was successfully created.',:id =>current_user.id )
  else
    flash[:success] = 'success'
    format.html { render action: "reserve" }
    format.json { render json: @reservation.errors.full_messages, status: :unprocessable_entity }
  end
end

從這里開始,事情開始變得混亂。 在我的預訂控制器中,每次我想要params [:id]時,我都沒有得到預訂ID。 我已經創建了新的預定並將其轉為行動預定。 [:id]似乎是nil或car_id,因為我擁有的鏈接是Reservation /:id(:format),而這個:id就是汽車ID而不是我的新預訂ID。 我的保留動作是Reservation.new

def reserve
@reservation = Reservation.new
@car = Car.find(params[:car_id])
if @car == nil
  flash[:danger] = "no car found"
else
  flash[:danger] = @car.id
end
respond_to do |format|
  format.html # new.html.erb
  format.json { render json: @reservation }
end
end

我在叢林中,一切都在樹林里糾結在一起。 在后備動作中,我可以通過car_id找到car,這是reservation /:id,此處為2。 但是在我的reserveConfirm中,我得到一個nil @car對象,它迫使我在找到所有具有id的汽車的地方使用,盡管只有一個原因導致id是唯一的。 更糟糕的是,在獲取@car之后,我想將其狀態更新為“保留”,但是當我查看db時,它永遠不會更改。

我的傳遞數據的表格在這里:

<%= form_for @reservation,:url => { :action => "reserveConfirm" } do |f| %>

<%=f.label :Date%>
<%=f.date_select :pick_up_time %>

<%= f.hidden_field :car_id, :value=> @car.id %>

<%= f.hidden_field :user_id, :value=> current_user.id %>

<%= f.submit "Confirm",  data: { confirm: 'Are you sure?', class: "btn btn-default" }%>

希望有人可以幫助我,非常感謝!

首先,您應該驗證您是否正確獲取@car。

我想您可以使用'byebug'。 嘗試在reserveConfirm方法的開頭編寫“ byebug”。

def reserveConfirm
    byebug
    #your code
end

使用byebug,您可以查看rails服務器(在終端中)並調試代碼。 嘗試編寫“參數”以檢查您收到的所有參數。 您可以使用byebug編寫“退出”或“繼續”。 (更多信息: 使用byebug進行調試

如果存在params [:car_id],則您的代碼應類似於:

@car = Car.find(params[:car_id])
@car.status = 'reserved'
if @car.update 
    #code
else
    #code
end

檢查一下,然后告訴我進展如何。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM