mysql - update_all 没有更新我的数据库

标签 mysql ruby-on-rails

我的预订数据库和汽车数据库之间存在多对多关系,以下内容在我的预订 Controller 中并被路由到帖子。

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

事情从这里开始变得困惑。在我的预订 Controller 中,每次我想要 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 找到汽车,这是 reservation/:id 归档的,这里是 2。但是在我的 reserveConfirm 中,我得到一个 nil @car 对象,这迫使我使用 where 找到所有带有 id 的汽车,尽管只有一个原因是 id 是唯一的。更糟糕的是,在我获得@car 之后,我想将其状态更新为保留状态,但是当我查看数据库时,它从未改变过。

传递数据的表单在这里:

<%= 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 服务器(在终端中)并调试您的代码。尝试编写“params”来检查您收到的所有参数。您可以使用 byebug 编写“退出”或“继续”。 (更多信息:Debugging using byebug)

如果 params[:car_id] 存在,你的代码应该是这样的:

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

检查并告诉我进展如何。

关于mysql - update_all 没有更新我的数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46453396/

相关文章:

ruby-on-rails - Rails - 带参数的成员操作 - 路径助手?

ruby-on-rails - rails 上的 ruby : audio/mp3 content header download

mysql - 帮助设置 Ruby on Rails 和 MySQL - 提供奖励

MySQL 查询在同一个表中使用 group by 计算零值

php - 如果 MySQL 数据库没有关闭会发生什么?

php - 在同一个 mysql 主机上,如何将 WordPress 帖子从一个数据库传输到另一个数据库?

mysql - 仅为单个数据库设置root无密码

javascript - rails : How to use minimised versions of css and js files in production and full versions in development?

ruby-on-rails - Rails 中外键约束和引用之间的区别

mysql - babel 为何在 Sequelize 中不起作用