我正在尝试从驾驶室索引页面获取预订控制器的新表单。我怎么才能拿到它?如何从驾驶室索引页面访问新表格,其中我已经显示了所有添加的驾驶室..
class CabsController < ApplicationController
before_action :find_cab, only: [:show, :edit, :update, :destroy]
def index
@cabs = Cab.all.order("created_at DESC")
end
def new
@cab = Cab.new
end
def show
@reviews = Review.where(cab_id: @cab.id).order("created_at DESC")
if @reviews.blank?
@avg_review=0
else
@avg_review=@reviews.average(:rating).round(2)
end
end
def edit
end
def create
@cab = Cab.new(cab_params)
if @cab.save
redirect_to @cab
else
render 'new'
end
end
def update
if @cab.update(cab_params)
redirect_to @cab
else
render 'edit'
end
end
def destroy
@cab.destroy
redirect_to root_path
end
private
def find_cab
@cab = Cab.find(params[:id])
end
def cab_params
params.require(:cab).permit(:name, :number_plate, :seat, :image)
end
end
class BookingsController < ApplicationController
before_action :find_booking, only: [:show, :edit, :update, :destroy]
before_action :find_cab
def index
@bookings = Booking.where(cab_id: @cab.id).order("created_at DESC")
end
def new
@booking = Booking.new
end
def show
end
def edit
end
def create
@booking = Booking.new(booking_params)
@booking.user_id = current_user.id
@booking.cab_id = @cab.id
if @booking.save
redirect_to cab_booking_path(@cab, @booking)
else
render 'new'
end
end
def update
end
def destroy
end
private
def find_booking
@booking = Booking.find(params[:id])
end
def find_cab
@cab = Cab.find(params[:cab_id])
end
def booking_params
params.require(:booking).permit(:date, :address, :start_destination, :destination, :start_date, :end_date, :contact_no)
end
end
resources :cabs do
resources :bookings
end
<div class="container">
<h2>All Cabs</h2>
<div class="row">
<% @cabs.each do |cab| %>
<div class="col-sm-6 col-md-3">
<div class="thumbnail">
<%= link_to image_tag(cab.image.url(:medium), class: 'image'), cab %><br>
Cab Name : <h4><%= cab.name %></h4>
<%= link_to "Book Now", new_cab_booking_path(@cab, @booking) %> # i wanted to create this link
</div>
</div>
<% end %>
</div>
<%= link_to "Add Cab", new_cab_path, class: 'btn btn-default' %>
No route matches {:action=>"new", :cab_id=>nil, :controller=>"bookings"} missing required keys: [:cab_id]
答案 0 :(得分:1)
没有路线匹配{:action =&gt;&#34; new&#34;,:cab_id =&gt; nil, :controller =&gt;&#34;预订&#34;}缺少必需的密钥:[:cab_id]
问题在于这一行
<%= link_to "Book Now", new_cab_booking_path(@cab, @booking) %>
应该是
<%= link_to "Book Now", new_cab_booking_path(cab) %>