【问题标题】:Rails error - ActionController::UrlGenerationError in BookingsController (Create action)Rails 错误 - BookingsController 中的 ActionController::UrlGenerationError(创建操作)
【发布时间】:2016-11-23 10:59:25
【问题描述】:

我正在使用 Rails 构建一个活动应用程序,但在预订确认过程结束时遇到了上述错误。这是完整的错误 -

这是我的控制器代码 -

预订控制器

class BookingsController < ApplicationController

    before_action :authenticate_user!



    def new

        @event = Event.find(params[:event_id])
        # and because the event "has_many :bookings"
        @booking = Booking.new(params[:booking])
        @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.booking
                flash[:success] = "Your place on our event has been booked"
                redirect_to event_booking_path(@event, @booking)
            else
                flash[:error] = "Booking unsuccessful"
                render "new"
            end

            if @event.is_free?
                @booking.save(booking_params)
            end
    end

    def show
        @event = Event.find(params[:event_id])
        @booking = Booking.find(params[:id])
    end


    def update

        if @booking.update(booking_params)
            redirect_to event_booking_path(@event, @booking) , 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






end

我正在尝试实现代码来处理免费和付费活动/预订。活动方面很好,但事实证明处理免费预订很麻烦。这是预订模型代码 -

Booking.rb

class Booking < ActiveRecord::Base

    belongs_to :event
    belongs_to :user


  before_create :set_booking_number 

     validates :quantity, presence: true, numericality: { greater_than_or_equal_to: 0 }
     validates :total_amount, presence: true, numericality: { greater_than_or_equal_to: 0 }
     validates :quantity, :total_amount, :booking_number, presence: true

  def set_booking_number
    self.booking_number = "MAMA" + '- ' + SecureRandom.hex(4).upcase
  end



    def booking
        # Don't process this booking if it isn't valid
        #self.valid?


        if self.event.is_free?

            self.total_amount = 0
        else



            begin
                        self.total_amount = event.price_pennies * self.quantity
                        charge = Stripe::Charge.create(
                            amount: total_amount,
                            currency: "gbp",
                            source: stripe_token, 
                            description: "Booking created for amount #{total_amount}")
                        self.stripe_charge_id = charge.id
              self.booking_number = "MAMA" + '- ' + SecureRandom.hex(4).upcase
                        save!
                    rescue Stripe::CardError => e
                    errors.add(:base, e.message)
                    false
                  end
            end 
        #end
  end
end

此问题是在包含验证后出现的 - 问题一直在尝试实施适用于付费活动但不是免费的验证。

对于上述错误的任何帮助将不胜感激。

【问题讨论】:

  • 此错误与型号无关。因此,添加/删除验证不会影响它。

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


【解决方案1】:

尝试替换

redirect_to event_booking_path(@event, @booking)

redirect_to event_booking_path(id: @booking.id, event_id: @event.id)

更新

我认为该错误是因为 @booking.id 正在评估为 nil。会发生@booking 未保存到数据库(仍然是新记录)。在您的模型方法booking 中,在if 块内,您只是在设置一个属性,而不是保存对象。因此,在设置属性后在 if 块内调用 save! 应该可以解决此问题。

def booking
  if self.event.is_free?
    self.total_amount = 0
    save!
  else
    ...
  end
end

【讨论】:

  • 不,这并没有改变任何东西。我认为 event.id 应该是 @event.event_id - 当我使用它时,我得到 event_id 的“未定义方法”错误。
  • 我包含模型的原因是 - 我更改了模型并且代码损坏了。我没有更改控制器,之前它可以正常工作,所以不知道为什么会这样。
  • 您是否仍然遇到问题中提到的错误?如果是,请发布rake routes | grep bookings的输出
  • 太长了,不能完整发帖,这个是show route - event_booking GET /events/:event_id/bookings/:id(.:format) bookings#show
  • 谢谢你 - 我也错过了保存!对于付费代码也 - 刚刚检查。我还调整了验证。现在一切正常。
【解决方案2】:

你需要使用正确的路线

Rails 期待 :id 而不是 :event_id

所以,如果你使用的是宁静的路线

使用网址作为booking_path(7) 而不是booking_path(event_id: 7)

或更改您的路线到

get 'bookings/:event_id', to: 'bookings#show'

【讨论】:

  • 我已尝试更改路线,但并没有改变任何东西。我不确定你对宁静路线方法的意思 - 很抱歉听起来很愚蠢,但数字 7 是从哪里来的?
  • 如果这是参数问题 - 我需要更改 create 方法中的任何参数吗?
猜你喜欢
  • 2017-03-29
  • 1970-01-01
  • 2017-03-04
  • 2015-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-30
相关资源
最近更新 更多