Rails错误 - BookingsController中的ActionController :: UrlGenerationError #create

时间:2016-11-15 13:15:14

标签: ruby-on-rails ruby ruby-on-rails-4 model-view-controller

我在使用Rails构建的事件应用中遇到上述错误。该错误与bookings_controller中的“show”操作有关,如此处所阐明的那样 -

'没有路由匹配{:action =>“show”,:controller =>“bookings”' - 错误消息。

预订系统旨在满足免费和付费预订。免费预订路线会根据需要转到展会页面。进行付费预订时会发生上述错误(通过Stripe付款)。这是相关的控制器代码 -

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

错误消息重点关注@ booking.paid_booking方法中的这一行 -

  redirect_to event_booking_path(@event)

这是奇怪的,因为它与free_booking使用相同的路由,路由到显示页面没有错误。

这是我的免费观看形式付费预订 -

     <% 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>  

这是路线档案 -

的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

路线 - Rake routes for bookings

我确信这是非常明显的事情,我担心我无法发现它。

2 个答案:

答案 0 :(得分:0)

你有嵌套资源和助手应该用两个参数调用。第一个是:event_id,第二个是:id。 :id可能是书ID

这样的事情:

 redirect_to event_booking_path(EVENT_ID, ID)

答案 1 :(得分:0)

所以,提供的答案之一 - 虽然它实际上没有工作 - 让我想到了参数,并且需要两个 - @event&amp; @booking。这就是我所做的修复 -

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, @booking)
        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, @booking)
        else
            flash[:error] = "Booking unsuccessful"
            render "new"
        end