Reputation: 5
So I'm working on a Rails application that involves a dog daycare. I'm trying to do a helper that will eventually aide in reservation validation but for now I'm just trying to get it to show all kennels from the Reservations table and put those names into an array
Here's the error I'm getting error image
Here's the code from the reservation helper:
module ReservationsHelper
def available_kennels
availability = Array.new
@r.all.each do|kennel|
availability.push(kennel)
end
return availability
end
end
In the reservation controller, I'm assigning @r to reservation.all
Here's the controller:
class ReservationsController < ApplicationController
def new
@reservation = Reservation.new
@d = current_user.dogs
@r = reservation.all
end
def create
@reservation = current_user.reservations.build(r_params)
if @reservation.save
flash[:success] = "Reservation has been made"
else
render new
end
end
def r_params
params.require(:reservation).permit(:dog_id, :kennel_id, :startdate, :enddate, :status, :report)
end
end
As for kennels, the only thing this table has the standard id and name.
I know I'm sort of close.
Upvotes: 0
Views: 54
Reputation: 148
Reservation is a class so it needs to be capitalized. I also recommend using reservations instead of r as your variable. It easier to read for new developers coming on board. Hopefully this helps you out:
reservations = Reservation.all
Your array is a little of is should be
@reservations.each do |reservation| // or @r in your case
In your code |kennel| is actually being used as an individual reservation not kennel's as I'm guessing that you want. If your relationships are correct and a reservation belongs_to a kennel you can:
def available_kennels(@reservations)
availability_array = []
@reservations.each do |reservation|
availability_array = availability_array.push(reservation.kennel.name)
end
return availability_array
end
Again, I changed the variable names a bit to help you understand what is going on. You are defining reservation when you put it in the pipes. To define an array in ruby you can just do array = []. Its a little different than javascript in that way.
If you want to get a list in a view you can also use a view template:
http://guides.rubyonrails.org/layouts_and_rendering.html
<ul>
@reservations.each do |reservation|
<li>
<%= reservation.kennel.name %>
</li>
end
</>ul
Upvotes: 1