ruby on rails - How to create booking according to its pitch? -
i have model pitch fetching grounddetail_id. want show pitch available in ground. how can 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 devise_for :users devise_for :admins resources :grounddetails 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 have pitch model , routes in controller confused use. can book pitch of ground. single ground able book.
in grounddetails#show
action
def show @pitches = @grounddetail.pitches end
then in groundetails/show.html.erb
, can use <%= @pitches %>
display pitches
of grounddetail
.
update:
#in show.html.erb <% @pitches.each |p| %> <%= form_tag book_pitch_path(p) %> #your attributes here <%= submit_tag "book pitch" %> <% end %> <% end %> #routes.rb post :book_pitch/:id, to: 'pitches/book_pitch', as: 'book_pitch' #in pitches_controller.rb def book_pitch #your actions here end
Comments
Post a Comment