【问题标题】:Can any one give me a hint how to get the spot_id?谁能给我一个提示如何获得spot_id?
【发布时间】:2021-12-15 03:27:08
【问题描述】:

我正在尝试制作停车场预订申请和一般​​预订工作。当我输入user idspot id 并选择is_booked。 现在我希望地点列表旁边的按钮以相同的方式工作,但我无法获得地点的 id,如下所示:

    <% @spots.each do |spot| %>
      <tr>
        <td><%= spot.name %></td>
        <td><%= link_to 'Show', spot %></td>
        <td><%= link_to 'Edit', edit_spot_path(spot) %></td>
        <td><%= link_to 'Booking', new_booking_path %></td>
      </tr>
    <% end %>
  </tbody>

目前的路径是new_booking,但仅用于预览,最终将是create_booking。

我尝试了几种方法,但都没有奏效,我可以引用所有 id 但不能引用单个 id。这是从 booking_controller 到 new_booking 定义的示例,我给出了这些参数:

@booking = current_user.bookings.build(:spot_id =&gt; Spot.ids, :is_booked =&gt; true)

我希望我已经清楚地描述了这个问题。我对红宝石相当陌生,这似乎是一个我不知道如何解决的小错误。请帮忙。

【问题讨论】:

    标签: ruby-on-rails ruby model-view-controller


    【解决方案1】:

    一种方法是将资源嵌套在 /config/routes.rb 文件中。

    这样关系将如下所示:

    resources :spots do
      resources :bookings
    end
    

    之后,如果您从命令行运行 rails routes,您现在将看到一个新路由 new_spot_booking_path,您可以在模板中将其用作 new_spot_booking_path(spot)。

    看看https://guides.rubyonrails.org/routing.html#nested-resources 和一般路由

    祝你好运!

    【讨论】:

      【解决方案2】:

      解决此问题的 Rails 方法是创建一个nested route

      resources :spots do
        resources :bookings, shallow: true
      end
      

      这将创建路径 /spots/:spot_id/bookings,这意味着现场 ID 作为 URL 的一部分传递。

      class BookingsController < ApplicationRecord
        before_action :set_spot, only: [:new, :create, :index]
        before_action :set_booking, only: [:show, :edit, :update, :destroy]
      
        # GET /spots/1/bookings/new
        def new
          @booking = @spot.bookings.new
        end
      
        # POST /spots/1/bookings
        def create
          @booking = @spot.bookings.new(booking_params) do |b|
            b.user = current_user
          end
          respond_to do |format|
            if @booking.save
              format.html { redirect_to @booking, notice: "Booking was successfully created." }
              format.json { render :show, status: :created, location: @booking }
            else
              format.html { render :new, status: :unprocessable_entity }
              format.json { render json: @booking.errors, status: :unprocessable_entity }
            end
          end
        end
      
        private
      
        def set_spot
          @spot = Spot.find(params[:spot_id])
        end
      
        def set_booking
          @booking = Booking.find(params[:id])
        end
      
        def booking_params
          params.require(:booking)
                .permit(:starts_at)
        end
      end
      
      # app/views/bookings/new.html.erb
      <%= render partial: 'form' %>
      
      # app/views/bookings/_form.html.erb
      <%= form_with(model: [@spot, @booking]) |form| %>
        # ...
        <%= form.submit %>
      <% end %>
      

      【讨论】:

      • 谢谢你,max,你发现我关于路由的错误。现在创建了正确的 URL,但我对 new 和 create 预订方法有疑问...undefined method `bookings' for #&lt;Spot id: 28, name:
      • 您需要将has_many :bookings 添加到您的Spot 模型中。 guides.rubyonrails.org/…
      • 非常好,它有效 :) 非常感谢
      • 如果它解决了您的问题,您应该接受答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-30
      • 1970-01-01
      • 1970-01-01
      • 2010-09-28
      • 1970-01-01
      相关资源
      最近更新 更多