无法从application_controller.rb找到没有ID的Model

时间:2017-12-19 16:41:51

标签: ruby-on-rails

我正在尝试通过在application_controller.rb文件中定义辅助方法为名为 Commuter 的模型创建会话。如下。

application_controller.rb

helper_method :current_commuter

  def current_commuter
      @commuter ||= Commuter.find session[:cid]
  end

在验证通勤者的电话号码后,我正在尝试创建一个 登录会话。

commuters_controller.rb

def verify
    @commuter = Commuter.where(phone_number: params[:phone_number]).first
    if (@commuter && @commuter.authenticate_otp(params[:otp],drift:300))
      @commuter.auth_active = true
      if @commuter.save
        #Removed from session after verified it
        session[:phone_number] = nil
        session[:is_verified] = nil
        #signed in commuter after verified it
        sign_in(:commuter, @commuter)
        flash[:notice] = "Your mobile no is verified."
      end
    else
      flash[:alert] = "You have entered wrong otp.Please check again."
    end
    puts "#{current_commuter.phone_number}"
    redirect_to root_path
  end

我添加puts来检查current_commuter是否有效。这是我在控制台上查看的时候。现在,当我在视图文件上添加如下链接:

应用/视图/预订/ outstation.html.erb

<%if current_commuter.present? %>
  <li> <%= link_to "Dashboard", dashboard_commuters_path %> </li>
  <li> <%= link_to "Logout", destroy_commuter_session_path, method: :delete%></li>
<%else%>
  <li>
    <a href="" data-toggle="modal" data-target="#signinmodal">
      REGISTER | LOGIN
    </a>
  </li>
<%end%>

现在我收到以下错误:

ActiveRecord::RecordNotFound in BookingsController#outstation

Couldn't find Commuter without an ID

修改

bookings_controller.rb

    class BookingsController < ApplicationController
  layout 'bookings'
  before_action :set_ride_later_request_from_ref_id, only:[
      :select_vehicle, :update_vehicle_details,
      :contact_details,:complete_booking_request]
  before_action :set_ride_later_request_from_uuid_ref, only:[
    :request_quotations,:quotations, :confirm_booking,
    :confirmation
  ]
  before_action :set_trip_type, only: [:outstation, :local_booking]

  def new_booking
    @ride_later_request = RideLaterRequest.new
    @vahicle =  RideLaterRequest.new
  end

  def create_ride_later_request
    @ride_later_request = RideLaterRequest.new(permitted_attributes(RideLaterRequest))
    respond_to do |format|
      if @ride_later_request.save
        format.html {redirect_to bookings_select_vehicle_path(ref_id: @ride_later_request.rlr_ref_code.content)}
      else
        format.html {render :outstation}
      end
    end
  end
  def update_vehicle_details
    respond_to do |format|
      if @ride_later_request.update(permitted_attributes(RideLaterRequest))
        format.html {redirect_to bookings_contact_details_path(ref_id:@ride_later_request.rlr_ref_code.content)}
      else
        format.html {render :select_vehicle}
      end
    end
  end

  def contact_details

  end

  def complete_booking_request
    @operation = nil
    if params[:get_otp].nil? && params[:get_quotations].nil?
      render json:{error:"Wrongly formed request"}, status: 422
    end
    if params[:get_otp]
      @operation = :get_otp
      if @ride_later_request.update(permitted_attributes(RideLaterRequest))
        SMSSender.send_otp_message(@ride_later_request.phone_number,@ride_later_request.otp_code)
        return
      else
        render json:{error:@ride_later_request.errors.full_messages[0]}, status: 422
      end
    elsif params[:get_quotations]
      @operation = :get_quotations
      if (params[:ride_later_request][:otp].blank? || 
        !@ride_later_request.authenticate_otp(params[:ride_later_request][:otp],drift:300))
          render json:{error:"OTP is wrong"}, status: 422 and return
      else
        @ride_later_request.user_verified = true
        if !@ride_later_request.save 
          render json:{error:@ride_later_request.errors.full_messages.first}, status:422 and return
        end
        if @ride_later_request.status == 'UnderConstruction'
          render json:{error:"Some fields are missing. Please try again"}, status:422  and return
        end
      end
    end
  end



  def request_quotations
    request_loc = [@ride_later_request.pickup_lat,@ride_later_request.pickup_lng]
    @rfq_agencies = @ride_later_request.rfq_agencies.sort_by{|l|l.distance_to(request_loc)}
    @ride_later_quotes = @ride_later_request.ride_later_quotes.to_a
    @ride_later_quotes.sort!{|a,b| a.total_payable.to_i <=> b.total_payable.to_i}
  end

  def quotations
    @ride_later_quotes = @ride_later_request.ride_later_quotes.to_a
    @ride_later_quotes.sort!{|a,b| a.total_payable.to_i <=> b.total_payable.to_i}
    if session[:trip_type] == 'HourlyPackage'
      render :local_booking_quotation
    else
      render :outstation_booking_quotations
    end  
  end

  def confirm_booking
    if @ride_later_request.status=='Unfulfilled' ||
        @ride_later_request.status=='Rejected' || 
        @ride_later_request.status=='Ignored'
      render json:{error:'Trip request has expired. Start a new booking.'}, status:422 and return
    end
    if @ride_later_request.status=="Cancelled"
      render json:{error:'Trip request has been cancelled. Start a new booking.'}, status:422 and return
    end
    if @ride_later_request.status!="QuotesReady"
      render json:{error:'Trip request is in wrong state. Start a new booking.'}, status:422 and return
    end

    @quote = @ride_later_request.ride_later_quotes.find(params[:quote_id]) rescue nil
    if @quote.blank?
      render json:{error:'Quotation not linked to this trip'}, status:422 and return
    end
    if @quote.status != 'Submitted'
      render json:{error: "Quote is in wrong state #{@quote.status}. Start a new booking"}, status:422 and return
    end
    @quote.status = 'Accepted'
    if !@quote.save
      render json:{error:@quote.errors.full_messages.first}, status:422 and return
    end
    RideLaterRequestHandler.commuter_accepts_quote(@quote)
  end

  def confirmation
    @ride = @ride_later_request.ride_later_ride
    @quote = @ride.ride_later_quote
  end


  def set_ride_later_request_from_ref_id
    @ref_id = params[:ref_id]
    @ride_later_request = RlrRefCode.where(content:@ref_id).first.ride_later_request rescue nil unless @ref_id.blank?    
    if @ride_later_request.blank?
      flash[:alert] = "Something went wrong. Please create new booking request."
      redirect_to bookings_new_path
    end    
  end

  def set_ride_later_request_from_uuid_ref
    @uuid = params[:booking_id]
    @ride_later_request = RideLaterRequest.where(uuid_ref:@uuid).first rescue nil if @uuid.present?
    if @ride_later_request.blank?
      flash[:alert] = "Something went wrong. Please create new booking request."
      redirect_to bookings_new_path
    end    
  end


  def select_vehicle
    @ride_later_request = RlrRefCode.find_by_content(params[:ref_id]).ride_later_request
  end

  def vehicle_type_ac
    if params[:booking_summary_id].present?
      @ride_later_request = RideLaterRequest.find(params[:booking_summary_id])
    else
      @ride_later_request = RlrRefCode.find_by_content(params[:ride_later_request][:ref_id]).ride_later_request
      @ride_later_request.update(vehicle_type: params[:ride_later_request][:vehicle_type], number_of_passengers: params[:ride_later_request][:number_of_passengers])
    end
  end

  def vehicle_type
    @ride_later_request = RlrRefCode.find_by_content(params[:ref_id]).ride_later_request
  end

  def outstation
    @ride_later_request = RideLaterRequest.new(trip_type: 'Outstation')
  end  

  def local_booking
    @ride_later_request = RideLaterRequest.new(trip_type: params[:trip_type])
  end

  def bangalore_innova
    @ride_later_request = RideLaterRequest.new(trip_type: 'Outstation')
  end
   def bangalore_outstation
    @ride_later_request = RideLaterRequest.new(trip_type: 'Outstation')
  end
   def bangalore_taxi
    @ride_later_request = RideLaterRequest.new(trip_type: 'Outstation')
  end


  def tt_for_rent
    @ride_later_request = RideLaterRequest.new(trip_type: 'Outstation')
  end
  def minibus_for_rent
    @ride_later_request = RideLaterRequest.new(trip_type: 'Outstation')
  end
  # def select_vehicle_local

  # end

  def about_us

  end

  def terms_conditions

  end
  def privacy_policy

  end
  def journey_way
    @ride_later_request = RideLaterRequest.create(user_params)
    @ride_later_request.save
    respond_to do |format|
      format.html {render :select_vehicle}
    end  
  end

  def outstation_booking_quotations
    @ride_later_request = RideLaterRequest.find_by(id: params[:ride_later_request])
    @ref_id = params[:ref_id]
  end

  def select_vehicle_outstation
    @ride_later_request = RlrRefCode.find_by_content(params[:ref_id]).ride_later_request
    redirect_to outstation_booking_quotations_path(@ride_later_request.rlr_ref_code.content, @ride_later_request )
  end

  def trip_summary

    @ride_later_request = RlrRefCode.find_by_content(params[:ride_later_request][:ref_id]).ride_later_request
    @ride_later_request.update(vehicle_type: params[:ride_later_request][:vehicle_type], number_of_passengers: params[:ride_later_request][:number_of_passengers])

    puts "Ride LATER REQUEST LOG"
    puts @ride_later_request
      if @ride_later_request.update(additional_instructions: params[:ride_later_request][:additional_instructions], ac: 'AC')
        render :template => 'bookings/booking_summary', :locals => {:ride_later_request => @ride_later_request, :ref_id => @ride_later_request.rlr_ref_code.content, :vehicle_type => params[:ride_later_request][:vehicle_type]}
      else
        format.html {render :index}
      end
  end

  def user_params
    params.require(:ride_later_request).permit(:pickup_area, :pickup_city, :destination_city, :pickup_lng, :pickup_lat)
  end

  def set_trip_type
    session[:trip_type] = params[:trip_type]
  end  
end

1 个答案:

答案 0 :(得分:0)

在我看来,这里抛出了错误:

$sql="insert into `$db_nombre2`.`cobranzaSemanal` ( `idInVoice`, `idUser`, `dueDate`, `status`, `firstName`, `lastName`, `phoneNumber` )
    (
        select v.`id`, v.`userid`, v.`duedate`, v.`status`, c.`firstname`, c.`lastname`, c.`phonenumber`
        from `$db_nombre`.`tblinvoices` v, `tblclients` c 
        where v.`duedate` > '2017-12-10'
    )";

您是将其称为def current_commuter @commuter ||= Commuter.find session[:cid] end 吗?

如果未找到记录,则使用before_action会引发错误,因此我建议您不要在find中传入ID,或者传递的内容与{不匹配}数据库中的{1}}

您可以使用session[:cid]来避免抛出错误(如果找不到记录,则会返回Commuter),或者解决此问题以确保方法仅被调用到应该的位置。

这有帮助吗?

修改:根据您的其他信息,这可能来自find_by_id中的任何nil。与上述相同的解释虽然:)