【问题标题】:How to find Booking without an ID如何在没有 ID 的情况下查找预订
【发布时间】:2020-03-11 03:09:22
【问题描述】:

我正在尝试销毁我的 Booking 表的一列。

我在 courses_controller.rb 中有一个连接表的索引,如下所示:

@bookings = Booking.joins(:schedule).where(user_id: current_user.id).where("schedules.status_id = ?", 11).page(params[:page]).per(5)

在视图中:

         <% @bookings.each.with_index(1) do |b, index| %>
            <tr>
              <th>
                <small><%= index %></small>
              </th>
              <td>
                <small><%= b.schedule.available.strftime("%b %e, %I:%M %p") %></small>
              </td>
              <td>
                <small></small>
              </td>
              <td>
                <small><%= b.schedule.tutor.name %></small>
              </td>
              <td>
                <span class="badge badge-pill badge-warning text-white text-capitalize">
                  <%= b.schedule.status.name %>
                </span>
              </td>
              <td>
                <%= link_to "Cancel request", users_bookings_path(b), method: :delete, data: {confirm: "Are you sure?"}, class: "btn btn-light btn-sm text-color-3 border" %>
              </td>
            </tr>
          <% end %>

在booking_controller.rb中,我有方法销毁:

class Users::BookingsController < ApplicationUserController
before_action :find_request, only: :destroy

def destroy
 @request.destroy
 flash[:info] = "Success cancel request"
 redirect_to users_requested_classes_path
end

private
def booking_params
 params.permit(:user_id, :status_id, :schedule_id)
end

def find_request
 @request = Booking.find(params[:id])
end


end

还有我的路线:

resource :bookings, only: :destroy

这是网络服务器控制台日志:

enter image description here

当我点击取消请求按钮时,它显示“找不到没有 ID 的预订”。为什么?

【问题讨论】:

  • 请显示从收到请求到出现错误的整个 Web 服务器控制台日志。我们需要查看您的参数等。
  • 完成。我已经更新了@Beartech 的帖子
  • 欢迎来到 SO!请阅读“How to Ask”,尤其是“拼写、语法和标点符号很重要!”部分。 SO 不是留言板或论坛,它更像是一本语法很重要的在线参考书。 “我” => “我”,shows Couldn't find... ID => shows "Couldn't find... ID"。我们花在编辑上的时间比我们必须帮助回答问题的时间少了。另外,请阅读“Discourage screenshots of code and/or errors”。

标签: ruby-on-rails ruby activerecord ruby-on-rails-5


【解决方案1】:

请注意您的日志中要求的:

users/bookings.4

在您下面的参数中,它显示唯一的参数作为真实性令牌?它还应该有一个 ID 参数。原因是您在表单中使用了错误的路径助手。

<%= link_to "Cancel request", users_bookings_path(b), method: :delete, data: {confirm: "Are you sure?"}, class: "btn btn-light btn-sm text-color-3 border" %>

生成

users/bookings.4

但如果你使用users_booking_path(b) 喜欢:

<%= link_to "Cancel request", users_booking_path(b), method: :delete, data: {confirm: "Are you sure?"}, class: "btn btn-light btn-sm text-color-3 border" %>

它应该生成正确的路径,例如:

users/bookings/4

【讨论】:

  • 始终阅读您的服务器日志,它们通常会告诉您问题所在。您必须真正寻找看起来不正常的东西。
  • 路径和复数/单数可能会变得复杂,尤其是如果英语不是您的第一语言。我会运行rake routes 来获取您所有路线的列表,它会告诉您哪些路线指的是单一资源等。
  • 是的.. 谢谢你的帮助。真的很感激,实际上是我的路线有问题。再次感谢兄弟
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-28
  • 2015-06-16
  • 1970-01-01
相关资源
最近更新 更多