I have a controller called BookingsController with a bookings#index action. Inside the index action, there are 2 instance variables, @pending_bookings
and @approved_bookings
, which query Booking objects by their status
.
def index
@pending_bookings = Booking.where(host_id:@user.id,
status:'pending')
@approved_bookings = Booking.where(host_id:@user.id,
status:'approved')
end
I want to route the user to a different instance of index depending on the link they click. Basically bookings_path(@pending_bookings)
should route the user to the index page displaying all pending_bookings
, adversely, bookings_path(@approved_bookings)
should route the user to the index page displaying all approved_bookings
.
In my view, I have 2 links that should direct the user to each path respectively.
<%= link_to 'Pending Reservations', bookings_path(@pending_bookings)%>
<%= link_to 'Approved Reservations', bookings_path(@approved_bookings)%> `
The index.html.erb file:
<%= booking_index_helper_path %>
contains an embedded helper method that should recognize the path the user clicks and render the proper Booking objects.
Here's the (flawed) logic for recognizing the path the user chooses and rendering the necessary objects:
pages_helper.rb:
def booking_index_helper_path
if bookings_path(@pending_bookings)
render @pending_bookings
elsif bookings_path(@approved_bookings)
render @approved_bookings
else bookings_path(@total_bookings)
@total_bookings
end
end
I put a binding.pry
in the helper method to confirm it is being hit (it is). For some reason when I click the link to direct me to the proper objects, however, the first condition is always satisfied. What is a better way to write this conditional to recognize the path the user chooses?
It seems like you're going about this in a more complicated way than you need to. Why not just have an index like:
def index
#Rails autoescapes this string so no fear of sql injection using user supplied strings
@bookings = Booking.where(host_id:@user.id, status: "#{params[:status]}")
end
Then use a link like:
<%= link_to 'Pending Reservations', bookings_path(status: 'pending')%>
<%= link_to 'Approved Reservations', bookings_path(status: 'approved')%> `
Now your view can just handle @bookings
and not concern itself with the types of @bookings
as that is done by the logic in your controller. This is the bare minimum but you should get in the habit of adding error messages etc. to your controllers so consider doing:
def index
if params[:status].present?
#Rails autoescapes this string so no fear of sql injection using user supplied strings
@bookings = Booking.where(host_id:@user.id, status: "#{params[:status]}")
flash[:success] = "#{params[:status].titleize} Bookings loaded."
redirect_to whatever_path
else
flash[:error] = "Something went wrong"
redirect_to some_path
end
end