1

I'm getting the error from line #7 of my views when I'm trying to create a new room:

undefined method `rooms_path' for #<#<Class:0x007fb46b5db2a0>:0x007fb46b5d9ec8>

I'm a little confused about why am I getting this error. I have built a relationship between Room and Facility that each Facility has many Rooms. The Facility part works bug free, I've been able to create/edit/show facilities.

Views (views/rooms/new.html.erb)

<div class="panel panel-default">
  <div class="panel-heading">
    Create your beautiful room
  </div>
  <div class="panel-body">
    <div class="container">
      <%= form_for @room do |f| %>
        <div class="form-group">
          <label>Name</label>
          <%=f.text_field :room_name, placeholder: "What is the name of the room?", class: "form-control", required: true%>
        </div>

        <div><%= f.submit "Create This Room", class: "btn btn-normal" %></div>

      <%end%>
    </div>
  </div>
</div>

My Room model (models/room.rb):

class Room < ApplicationRecord
  belongs_to :facility

  validates :room_type, presence: true
  validates :room_name, presence: true
end

My Facility model (models/facility.rb):

class Facility < ApplicationRecord
  belongs_to :user
  has_many :photos
  has_many :rooms

  validates :facility_name, presence: true
  validates :address, presence: true
  validates :license, presence: true
  validates :phone, presence: true

  def cover_photo(size)
    if self.photos.length > 0
      self.photos[0].image.url(size)
    else
      "blank.jpg"
    end
  end
  
end

My Rooms Controller(controllers/rooms_controller.rb)

class RoomsController < ApplicationController
  before_action :set_room, except: [:index, :new, :create]
  
  def index
    @facility = Facility.find(params[:facility_id])
    @rooms = @facility.rooms
  end

  def new
    @facility = Facility.find(params[:facility_id])
    @room = @facility.rooms.build
  end
 end

Routes (routes.rb)

Rails.application.routes.draw do
  devise_for :users,
              path: '',
              path_names: {sign_in: 'login', sign_out: 'logout', edit: 'profile', sign_up: 'registration'},
              controllers: {omniauth_callbacks: 'omniauth_callbacks', registrations: 'registrations'}

  resources :users, only: [:show]
  resources :facilities, except: [:edit] do
    member  do
      get 'listing'
      get 'pricing'
      get 'features'
      get 'services'
      get 'types_care'
      get 'photo_upload'
      get 'room_upload'
    end

    resources :rooms, except: [:edit] do
      member  do
        get 'listing'
        get 'pricing'
        get 'photo_upload'
      end
    end

    resources :photos, only: [:create, :destroy]
  end
end

Been stuck on this for a couple of days and would really appreciate the help :) Thanks so much in advance!

Tony
  • 9,672
  • 3
  • 47
  • 75
Stella Han
  • 161
  • 1
  • 7

2 Answers2

0

form_for generate url based on class of @room, it doesn't include outer resource. Thus, you have to tell Rails to do that explicitly using below statement:

<%= form_for [@room.facility, @room] do |f| %>
Tai
  • 1,244
  • 9
  • 11
0

Choose any of the following for form_for instead of <%= form_for @room do |f| %> as your routes are nested

<%= form_for @room, url: [@room.facility, @room] do |f| %>

<%= form_for @room, url: [:facility, @room] do |f| %>

<%= form_for [@room.facility, @room] do |f| %>

<%= form_for [:facility, @room] do |f| %>

Here you can get more detail about form_for helper

Ganesh
  • 1,924
  • 1
  • 18
  • 31