jeudi 28 janvier 2016

How to create booking according to its pitch?

I have a model pitch where i am fetching grounddetail_id. I want to show all the pitch availabe in the ground. How i can show and book pitch of ground..

grounddetails_controller.rb

class GrounddetailsController < ApplicationController

 before_action :find_ground, only: [:show, :edit, :destroy, :update]

def index
 @grounddetails = Grounddetail.all.order('created_at DESC')
end

def new
 @grounddetail = Grounddetail.new
end

def edit
end

def show

end 

def create
  @grounddetail = Grounddetail.new(ground_params)
  if @grounddetail.save
   redirect_to @grounddetail
  else
   render 'new'
 end
end

def update
  if @grounddetail.update(ground_params)
    redirect_to @grounddetail
  else
    render 'edit'
  end
end

def destroy
  @grounddetail.destroy
   redirect_to root_path
end


private

  def find_ground
   @grounddetail = Grounddetail.find(params[:id])
  end

  def ground_params
   params.require(:grounddetail).permit(:name, :working_hours, :end_time, :address, :contact_no, :email, :number_of_grounds, :description, :featured_ground)
  end
end

routes.rb

Rails.application.routes.draw do

 devise_for :users
 devise_for :admins

 resources :features
 resources :grounddetails do 
   resources :pitches
 end


 root "grounddetails#index"
end

model

grounddetail.rb

class Grounddetail < ActiveRecord::Base
 has_many :pitches, dependent: :destroy
end

pitch.rb

class Pitch < ActiveRecord::Base
 belongs_to :grounddetail
end

for now i just have pitch model and routes but in controller i am confused what to use. i can i book pitch of the ground. But for single ground i am able to book.

Aucun commentaire:

Enregistrer un commentaire