0

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.

2
  • 1
    You should use Reservation.all instead of reservation.all. Commented Apr 19, 2018 at 15:20
  • Also, I would suggest you take the time to properly name your variables. @d and @r may save you a few key strokes. But, IMO, it can make your code harder to read and follow (for the future you and potential collaborators). Commented Apr 19, 2018 at 15:25

1 Answer 1

1

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     
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! This was such a huge help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.