0
我想從駕駛室索引頁面獲取預訂控制器的新窗體。我怎麼取回它?如何訪問從駕駛室索引頁的新形式,在那裏我已經證明所有這些都增加了出租車..如何從rails的父索引頁訪問子新窗體?
駕駛室控制器
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
@[email protected](: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
cab/index.html.erb
<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]
更新您的問題與'駕駛室/ index.html.erb'代碼。 – Pavan
您的請求路徑應類似'/ cabs /:cab_id/bookingings/new'。是嗎? – Krule
是的多數民衆贊成路徑@Krule –