2016-02-24 37 views
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] 
+1

更新您的问题与'驾驶室/ index.html.erb'代码。 – Pavan

+0

您的请求路径应类似'/ cabs /:cab_id/bookingings/new'。是吗? – Krule

+0

是的多数民众赞成路径@Krule –

回答

1

没有路由匹配{:动作=> “新”,:cab_id =>零, :控制器=> “预订” }缺少必需的键:[:cab_id]

问题是与这条线

<%= link_to "Book Now", new_cab_booking_path(@cab, @booking) %> 

这应该是

<%= link_to "Book Now", new_cab_booking_path(cab) %> 
+1

非常感谢...工作! –

相关问题