Couldn't find Model without an ID from application_controller.rb - ruby-on-rails

I am trying to create a session for a model called Commuter by defining a helper method in application_controller.rb file. As follows.
application_controller.rb
helper_method :current_commuter
def current_commuter
#commuter ||= Commuter.find session[:cid]
end
After verifying the commuter's phone number I am trying to create a
login session.
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
I put a puts to check if the current_commuter is working or not. It is when I checked on the console. Now when I added few links on a view file as follows:
app/views/bookings/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%>
Now I am getting the following error:
ActiveRecord::RecordNotFound in BookingsController#outstation
Couldn't find Commuter without an ID
EDIT
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

It looks to me like the error's thrown here:
def current_commuter
#commuter ||= Commuter.find session[:cid]
end
Are you calling this as a before_action?
Using find raises an error if a record's not found, so I'd suggest you're either not passing in an id in session[:cid], or what you are passing doesn't match a Commuter in your db.
You can use find_by_id to avoid throwing an error (it returns nil if the record's not found), or work around this to ensure the method's only called where it should be.
Does that help?
Edit: Based on your additional info, this could be coming from any of the finds in the bookings_controller. Same explanation as above though :)

Related

Is it possible to chain strong parameters in a Rails controller

I'm currently doing this
class DetailsController < ApplicationController
def create
#detail = Detail.new(params.require(:detail).permit(:user_id, :note))
if #detail.save
redirect_to #detail
else
render 'new'
end
end
def update
#detail = Detail.find(params[:id])
if #detail.update(detail_params)
redirect_to #detail
else
render 'edit'
end
end
private
def detail_params
params.require(:detail).permit(:note)
end
end
Is it possible too chain strong parameters in Rails? I tried doing this but it didn't work
class DetailsController < ApplicationController
def create
#detail = Detail.new(detail_params.permit(:user_id))
if #detail.save
redirect_to #detail
else
render 'new'
end
end
def update
#detail = Detail.find(params[:id])
if #detail.update(detail_params)
redirect_to #detail
else
render 'edit'
end
end
private
def detail_params
params.require(:detail).permit(:note)
end
end
To DRY things up I have used the tap method and done something like this...
def detail_params(is_create = false)
params.require(:detail).permit(:note).tap do |p|
p[:user_id] = current_user.id if is_create
end
end
def create
#detail = Detail.new(detail_params(true))
if #detail.save
redirect_to #detail
else
render 'new'
end
end
def update
#detail = Detail.find(params[:id])
if #detail.update(detail_params)
redirect_to #detail
else
render 'edit'
end
end
To just have different sets of parameters, this works...
def detail_params(is_create = false)
permitted = [:note]
permitted << :user_id if is_create
params.require(:detail).permit(permitted)
end

Undefined Method Error in controller

models/collaborators.rb:
class Collaborator < ActiveRecord::Base
belongs_to :user
belongs_to :wiki
def wiki_collaborations
end
end
controllers/wikis_controller.rb:
class WikisController < ApplicationController
def index
#user = current_user
if #user.admin?
#wikis = Wiki.all
elsif #user.premium?
#wikis = Wiki.where(private: false) | #user.wiki_collaborations | #user.wikis
elsif #user.standard?
#wikis = Wiki.where(private: false) | #user.wiki_collaborations
else
#wikis = Wiki.where(private: false)
end
end
def show
#wiki = Wiki.find(params[:id])
authorize #wiki
end
def new
#wiki = Wiki.new
end
def create
#wiki = current_user.wikis.new(wiki_params)
if #wiki.save
flash[:notice] = "Wiki was saved."
redirect_to #wiki
else
flash.now[:alert] = "There was an error saving the wiki. Please try again."
render :new
end
end
def edit
#user = current_user
#wiki = Wiki.find(params[:id])
#user_emails = User.where.not(id: current_user.id || #wiki.users.pluck(:id)).map(&:email)
authorize #wiki
end
def update
#wiki = Wiki.find(params[:id])
authorize #wiki
if #wiki.update(wiki_params)
flash[:notice] = "Wiki was updated."
redirect_to #wiki
else
flash.now[:alert] = "There was an error saving the wiki page. Please try again."
render :edit
end
end
def destroy
#wiki = Wiki.find(params[:id])
if #wiki.destroy
flash[:notice] = "\"#{#wiki.title}\" was deleted successfully."
redirect_to wikis_path
else
flash.now[:alert] = "There was an error deleting the wiki page."
render :show
end
end
private
def wiki_params
params.require(:wiki).permit(:title, :body, :private)
end
end
I tried to access http://localhost:3000/wikis.
I get the following error.
Possibly you've not defined wiki_collaborations method for user.
You can delegate it to User like,
class User < ActiveRecord::Base
delegate :wiki_collaborations, to: :collaborator
end
Btw, be aware of | is not same as ||.

No route matches, missing required keys

I'm currently working on rails 5 project in which I want to add a passenger to a transportation. In the in the passengers controller index action I set transportation to be search by the params id. But it returns nil even though it has been created.
Here are the routes:
user_protest_transportation_passengers GET /users/:user_id/protests/:protest_id/transportations/:transportation_id/passengers(.:format) passengers#index
POST /users/:user_id/protests/:protest_id/transportations/:transportation_id/passengers(.:format) passengers#create
new_user_protest_transportation_passenger GET /users/:user_id/protests/:protest_id/transportations/:transportation_id/passengers/new(.:format) passengers#new
edit_user_protest_transportation_passenger GET /users/:user_id/protests/:protest_id/transportations/:transportation_id/passengers/:id/edit(.:format) passengers#edit
user_protest_transportation_passenger GET /users/:user_id/protests/:protest_id/transportations/:transportation_id/passengers/:id(.:format) passengers#show
PATCH /users/:user_id/protests/:protest_id/transportations/:transportation_id/passengers/:id(.:format) passengers#update
PUT /users/:user_id/protests/:protest_id/transportations/:transportation_id/passengers/:id(.:format) passengers#update
DELETE /users/:user_id/protests/:protest_id/transportations/:transportation_id/passengers/:id(.:format) passengers#destroy
user_protest_transportations GET /users/:user_id/protests/:protest_id/transportations(.:format) transportations#index
POST /users/:user_id/protests/:protest_id/transportations(.:format)
Here is the link:
<div class="card">
<div class="card-block">
<h3 class="card-title">Need A Ride?</h3>
<p class="card-text">Find people near you willing to share a seat who are coming to the event. We are all in this together!.</p>
<%= link_to 'Get A Ride', new_user_protest_transportation_passenger_path(#user, #protest, #transportation), class: "btn btn-primary" %>
</div>
</div>
Here is the passengers controller:
class PassengersController < ApplicationController
def index
#transportation = Transportation.find(params[:transportation_id])
end
def new
end
def create
#passenger = Passenger.new(passenger_params)
#user = User.find(current_user.id)
#transportation = Transportation.find(params[:transportation_id])
#driver = User.find(#transportation.transporter_id)
#protest = Protest.find(#transportation.destination_id)
#transportations = Transportation.where(destination_id: #protest.id)
has_ride = false
#transportations.each do |transportation|
passenger = Passenger.find_by user_id: current_user.id
unless (passenger.nil?)
has_ride = true
end
end
if has_ride
redirect_to protests_path, notice: "You already have a ride."
return
end
respond_to do |format|
if #passenger.save
format.html { redirect_to user_protest_transportation_passenger_path(id: #passenger.id), notice: 'Passenger was successfully created.' }
else
format.html { render :new }
end
end
end
def show
#driver = get_driver
if #driver
#transportation = Transportation.where(transporter_id: #driver.id).first!
#protest = Protest.find(#transportation.destination_id)
end
end
def edit
end
def update
end
def destroy
#passenger = Passenger.find_by(user_id: params[:user_id], transportation_id: params[:transportation_id])
if #passenger
#passenger.destroy
redirect_to user_protests_path, notice: 'Ride was canceled.'
else
redirect_to user_protest_transportations_path, notice: 'Error: the passenger record failed to delete.'
end
end
private
def get_driver
driver = nil
p = Passenger.find_by(user_id: current_user.id, transportation_id: params[:transportation_id])
t = Transportation.find(params[:transportation_id])
driver = User.find(t.transporter_id)
end
def passenger_params
params.permit(:user_id, :transportation_id)
end
end
I am trying to get the transportation id in the params, I tried find with session[transportation_id] but didn't seem to work.
protest controller:
class ProtestsController < ApplicationController
def index
#protests = Protest.all
end
def show
#protest = Protest.find(params[:id])
#user = current_user
end
def new
#protest = Protest.new
end
def edit
#protest = Protest.find(params[:id])
end
def create
#protest = Protest.new(protest_params)
#protest.creator = current_user
respond_to do |format|
if #protest.save
format.html { redirect_to #protest, notice: 'Protest was successfully created.' }
else
format.html { render :new }
end
end
end
def update
#protest = Protest.find(params[:id])
respond_to do |format|
if #protest.update(protest_params)
format.html { redirect_to #protest, notice: 'Protest was successfully updated.' }
else
format.html {render :edit }
end
end
end
def destroy
#protest = Protest.find(params[:id])
#protest.destroy
redirect_to protests_path
end
private
def protest_params
params.require(:protest).permit(:name, :description, :location, :starts_at, :creator, :image)
end
end

DRY Rails controller to avoid repeating multiple times same method

I would like to DRY my controller as I use some snippets/blocks of code many times inside the same controller (I have removed some as it was too long but this already gives an idea of the repetition). Here are the blocks I keep repeating :
#deal = search_deal
#next_deal = find_next_deal
#userdeal = find_or_create_userdeal_participation
#user_credits = calculate_user_credits_in_deal
I'm quite rookie and don't know how to do this but I feel this code should be factorized.
class DealsController < ApplicationController
before_filter :find_deal,
:only => [ :showcase ]
before_filter :ensure_canonical_deal_path!,
:only => [ :showcase ]
def showcase
# find appropriate deal
#deal = search_deal
respond_to do |format|
format.html # showcase.html.erb
format.json { render json: #deal }
end
end
def buy_stuff
#deal = search_deal
# bring 'next deal' url to the view
#next_deal = find_next_deal
# USER IS SIGNED-IN
if user_signed_in?
#userdeal = find_or_create_userdeal_participation
#user_credits = calculate_user_credits_in_deal
# if: user still has credits available
if #user_credits >= 1
#do this
respond_to do |format|
format.js
end
else
respond_to do |format|
# do that
end
end
# USER IS NOT SIGNED-IN
else
respond_to do |format|
format.js { render :template => "deals/call_to_sign_in.js.erb" }
end
end
end
def show_discounts
#deal = search_deal
respond_to do |format|
#do that
end
end
def pending_deals
#deal = search_deal
# bring 'next deal' url to the view
#next_deal = find_next_deal
if user_signed_in?
#userdeal = find_or_create_userdeal_participation
#user_credits = calculate_user_credits_in_deal
end
respond_to do |format|
#do this
end
end
def ask_question
#deal = search_deal
respond_to do |format|
#do that
end
end
protected
def ensure_canonical_deal_path!
if request.path != actual_deal_page_path(#deal)
redirect_to actual_deal_page_path(#deal, :format => params[:format]), :status => :moved_permanently
return false
end
end
private
# DRY file as this is used multiple times
# trick source - http://blog.rstankov.com/rails-anti-pattern-setting-view-variables-in-before-actions/
def search_deal
Deal.friendly.find(params[:id])
end
def find_or_create_userdeal_participation
UserDeal.where('user_id = ? AND deal_id = ?', current_user.id, #deal.id).take ||
UserDeal.create(user_id: current_user.id, deal_id: #deal.id)
end
def calculate_user_credits_in_deal
current_user.credit_nb + #userdeal.history
end
def find_next_deal
Deal.order_next_deal_at(#deal).next
end
end
I think the best way to just add before_filters for those methods where you are calling repeat code like:
before_filter :search_deal, :only => [:showcase, :buy_stuff, ...]
class DealsController < ApplicationController
before_filter :find_deal, only: [:showcase]
before_filter :ensure_canonical_deal_path!, only: [:showcase]
def showcase
search_deal
respond_to do |format|
format.html
format.json{render json: #deal}
end
end
def buy_stuff
search_deal
find_next_deal
if user_signed_in?
find_or_create_userdeal_participation
calculate_user_credits_in_deal
if #user_credits >= 1
respond_to(&:js)
else
respond_to{|format| }
end
else
respond_to{|format| format.js{render template: "deals/call_to_sign_in.js.erb"}}
end
end
def show_discounts
search_deal
respond_to{|format|}
end
def pending_deals
search_deal
find_next_deal
if user_signed_in?
find_or_create_userdeal_participation
calculate_user_credits_in_deal
end
respond_to{|format| }
end
def ask_question
search_deal
respond_to{|format| }
end
protected def ensure_canonical_deal_path!
if request.path != actual_deal_page_path(#deal)
redirect_to actual_deal_page_path(#deal, format: params[:format]), status: :moved_permanently
return false
end
end
private def search_deal
#deal = Deal.friendly.find(params[:id])
end
private def find_or_create_userdeal_participation
#user_deal =
UserDeal.where('user_id = ? AND deal_id = ?', current_user.id, #deal.id).take ||
UserDeal.create(user_id: current_user.id, deal_id: #deal.id)
end
private def calculate_user_credits_in_deal
#user_credits = current_user.credit_nb + #userdeal.history
end
private def find_next_deal
#next_deal = Deal.order_next_deal_at(#deal).next
end
end

The action 'destory' could not be found in controller

I think I'm losing it. I can't figure out why a simple destroy link wont work all of a sudden.
I'm getting this error:
AbstractController::ActionNotFound at /plans/v41w74v2
The action 'destroy' could not be found for PlansController
Plan Controller:
class PlansController < ApplicationController
before_filter :authenticate_user!
def index
#plans = Plan.all
end
def new
#plan = Plan.new
end
def create
#plan = Plan.new(plan_params)
if #plan.save
if #plan.trips.empty?
#possible = #plan.group.trips
render "add_trips"
else
redirect_to plans_path, notice: "Plan successfully created"
end
else
render "new"
end
end
def show
#plan = Plan.find_by_unique_identifier(params[:id])
end
def edit
#plan = Plan.find_by_unique_identifier(params[:id])
#group = #plan.group
#possible = #plan.group.trips
render "add_trips"
end
def update
#plan = Plan.find_by_unique_identifier(params[:id])
if #plan.update_attributes(plan_params)
redirect_to plans_path, notice: "Plan successfully updated!"
else
render "edit"
end
end
def add_trips
#plan = Plan.find_by_unique_identifier(params[:id])
trip.each.update_attributes(plan_id: #plan.id)
redirect_to plans_path, notice: "Trips added to plan"
end
def destory
#plan = Plan.find_by_unique_identifier(params[:id])
#plan.destroy
end
Plan show View:
<%= button_to "Delete", #plan, method: :delete, data: { confirm: "Are you sure you want to delete this plan?"} %>
What am I doing wrong?
Yep. You've got a typo. destory.

Resources