Mike.Whitehead Mike.Whitehead - 25 days ago 7
Ruby Question

Rails error - ActionController::UrlGenerationError in BookingsController#create

I have the above error on my Events app I'm building using Rails. The error relates to the 'show' action in the bookings_controller as clarified here -

'No route matches {:action=>"show", :controller=>"bookings" ' - on the error message.

The booking system aims to cater for both free and paid bookings. The free bookings routes go to the show page as required. The above error happens when making a paid booking (payments via Stripe). This is the relevant controller code -

bookings_controller.rb

def new
@event = Event.find(params[:event_id])
@booking = @event.bookings.new(quantity: params[:quantity])
# which person is booking the event?
@booking.user = current_user



end

def create

@event = Event.find(params[:event_id])
@booking = @event.bookings.new(booking_params)
@booking.user = current_user

if
@booking.paid_booking
flash[:success] = "Your place on our event has been booked"
@booking.update_attributes!(booking_number: "MAMA" + '- ' + SecureRandom.hex(4).upcase)
redirect_to event_booking_path(@event)
else
flash[:error] = "Booking unsuccessful"
render "new"
end
end

def free_booking
if
@booking.free_booking
@booking.update_attributes!(booking_number: "MAMA" + '- ' + SecureRandom.hex(4).upcase)
redirect_to event_booking_path(@event)
else
flash[:error] = "Booking unsuccessful"
render "new"
end
end

def show
@event = Event.find(params[:event_id])
@booking = @event.bookings.new
@booking = Booking.find_by(params[:booking_number])
end

def update
puts params
@event = Event.find(params[:event_id])
@booking = @event.bookings.new(booking_params)


if @booking.save
redirect_to event_booking_path , notice: "Booking was successfully updated!"
else
render 'new'
end
end




private

def booking_params
params.require(:booking).permit(:stripe_token, :booking_number, :quantity, :event_id, :stripe_charge_id, :total_amount)
end


The error message focuses on this line in the @booking.paid_booking method -

redirect_to event_booking_path(@event)


This is odd as its the same route used for free_booking which routes through to the show page with no errors.

This is my view forms for free & paid bookings -

<% if @booking.free_booking %>
<div class="col-md-6 col-md-offset-3" id="eventshow">
<div class="row">
<div class="panel panel-default">
<div class="panel-heading">
<h2>Confirm Your Booking</h2>
</div>



<%= simple_form_for [@event, @booking], id: "new_booking" do |form| %>
<% if @booking.errors.any? %>
<h2><%= pluralize(@booking.errors.count, "error") %> prevented this Booking from saving:</h2>
<ul>
<% @booking.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
<% end %>

<div class="form-group">
<p>Please confirm the number of spaces you wish to reserve for this event.</p>
<%= form.input :quantity, class: "form-control" %>
</div>
<p> This is a free event. No payment is required.</p>

<div class="panel-footer">


<%= form.submit :submit, label: 'Confirm Booking', class: "btn btn-primary" %>

<% end %>
</div>


<% else %>

<div class="col-md-6 col-md-offset-3" id="eventshow">
<div class="row">
<div class="panel panel-default">
<div class="panel-heading">
<h2>Confirm Your Booking</h2>
</div>



<%= simple_form_for [@event, @booking], id: "new_booking" do |form| %>

<div class="calculate-total">
<p>
Confirm number of spaces you wish to book here:
<input type="number" placeholder="1" name="booking[quantity]" min="1" value="1" class="num-spaces">
</p>
<p>
Total Amount
£<span class="total" data-unit-cost="<%= @event.price %>">0</span>
</p>
</div>



<span class="payment-errors"></span>

<div class="form-row">
<label>
<span>Card Number</span>
<input type="text" size="20" data-stripe="number"/>
</label>
</div>

<div class="form-row">
<label>
<span>CVC</span>
<input type="text" size="4" data-stripe="cvc"/>
</label>
</div>

<div class="form-row">
<label>
<span>Expiration (MM/YYYY)</span>
<input type="text" size="2" data-stripe="exp-month"/>
</label>
<span> / </span>
<input type="text" size="4" data-stripe="exp-year"/>
</div>
</div>
<div class="panel-footer">

<%= form.button :submit %>


</div>

<% end %>
<% end %>

</div>
</div>
</div>


This is the routes file -

routes.rb

Rails.application.routes.draw do



devise_for :users, :controllers => { omniauth_callbacks: "omniauth_callbacks", registrations: "registrations" }


resources :users
# the above resource draws out routes for user profiles
resources :events do

resources :comments
resources :bookings
end





root 'events#index'



end


And routes -
Rake routes for bookings

I'm sure this is something quite obvious put I'm afraid I can't spot it.

Answer

You have nested resource and helper should be call with two params. The first one is :event_id, the second one :id. The :id its probably book id

Something like this:

 redirect_to event_booking_path(EVENT_ID, ID)