简体   繁体   中英

update_all not updating my database

I have a many-to-many relationship between my reservation db and cars db, and the following is in my reservation controller and is routed to the post.

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

things start to get confusing from here. In my reservation controller, every time i want params[:id], i am not getting the reservation id. I have my new reservation created and routed to get in action reserve. The [:id] seems to either be nil or a car_id, since the link i have is reservation/:id(:format), and this :id is somehow the cars id instead of my new reservation id. My reserve action does 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

I am in the jungle and everything tangles up in the woods. In reserve action, I can find car by car_id which is the reservation/:id filed, which is 2 here. But in my reserveConfirm, i am getting a nil @car object, which forces me to use where that finds all car with id , although only one cause the id is unique. And worse, after i get @car, i want to update its status to reserved, but when i look into db, it is not ever changed.

My form, which passes data is here:

<%= 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" }%>

Hope someone can kindly help me with this, much appreciate!

First of all, you should verify if you are getting @car correctly.

I guess you are able to use 'byebug' . Try writing 'byebug' at beginning of reserveConfirm method.

def reserveConfirm
    byebug
    #your code
end

Using byebug, you can look your rails server (in terminal) and debug your code. Try writing 'params' to check all params that you are receiving. You can write 'exit' or 'continue' using byebug. (More info: Debugging using byebug )

If params[:car_id] exists, your code should be like:

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

Check that and tell me how it goes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM