1

I´m new to Rails and can´t find any solution to my problem with my namespace I created.

The problem is that if I like to show all my rooms under localhost:3000/manage/ it does not work.

error: undefined method `title' for nil:NilClass.

If I try to show all my rooms under ../manage/rooms/ it is still not showing all rooms I created error: can´t find id... if I use an id e.g. ../manage/rooms/38 it is working. Hope someone can help me. So here we go: I created a namespace like: routes.rb:

resources :manage

namespace :manage do
 resources :rooms
end

Rake routes:

  manage_index GET    /manage(.:format)                  manage#index
                     POST   /manage(.:format)                  manage#create
          new_manage GET    /manage/new(.:format)              manage#new
         edit_manage GET    /manage/:id/edit(.:format)         manage#edit
              manage GET    /manage/:id(.:format)              manage#show
                     PATCH  /manage/:id(.:format)              manage#update
                     PUT    /manage/:id(.:format)              manage#update
                     DELETE /manage/:id(.:format)              manage#destroy
        manage_rooms GET    /manage/rooms(.:format)            manage/rooms#index
                     POST   /manage/rooms(.:format)            manage/rooms#create
     new_manage_room GET    /manage/rooms/new(.:format)        manage/rooms#new
    edit_manage_room GET    /manage/rooms/:id/edit(.:format)   manage/rooms#edit
         manage_room GET    /manage/rooms/:id(.:format)        manage/rooms#show
                     PATCH  /manage/rooms/:id(.:format)        manage/rooms#update
                     PUT    /manage/rooms/:id(.:format)        manage/rooms#update
                     DELETE /manage/rooms/:id(.:format)        manage/rooms#destroy

a controller under ../manage/rooms_controller.rb

class Manage::RoomsController < ApplicationController
 def index
   @rooms = Room.all
 end
 def new
  @room = Room.new
 end

def create
  #ender text: params[:rooms].inspect
  #render text: Rails.logger.info(params[:rooms].inspect)
  @room = Room.new(room_params)

if @room.save
  redirect_to [:manage, @room]
else
  render 'new'
end

end

def show
 @room = Room.find(params[:id])
end

def edit
 @room = Room.find(params[:id])
end

def update
 @room = Room.find(params[:id])

if @room.update(params[:rooms].permit(:number_of_rooms, :occupancy, :room_type, :gender, :title))
  redirect_to [:manage, @room]
else
  render 'edit'
end
end

def destroy
 @room = Room.find(params[:id])
 @room.destroy

 redirect_to manage_index_path
end

private
 def room_params
   params.require(:rooms).permit(:number_of_rooms, :occupancy, :room_type, :gender, :title)
 end
end

and under ... manage/rooms/_index.html.erb it is rendered in /layouts/_navigation_left.html.erb

Thanks! If you need more information just ask.

Thanks sorry I´m quite new to Rails. How do I change the controller / routes so I can manage not just @rooms but even @foo, @fee ...? Thanks.

I try to use: I do like to show <%= @room.title %> <%= @room.number_of_rooms %>or do you need something else? Sorry! and <%= render 'manage/rooms/index'%>

mmvsbg
  • 3,570
  • 17
  • 52
  • 73
oliver
  • 133
  • 2
  • 12
  • Do you have a ManageController? If not, you should remove the resources route for that. – James Mason May 03 '14 at 17:44
  • Hmm. Wish I was at a computer to test. Try changing the order of your routes. That should keep rails from thinking "room" is a model ID. – James Mason May 03 '14 at 17:53
  • @ room isn't going to exist in the index view. Try @ rooms.size to show the number of rooms. – James Mason May 03 '14 at 18:09
  • /manage is routing to ManageController# index. If you want @ rooms there, you'll need to modify that controller or change which controller the routes point to. – James Mason May 03 '14 at 19:14
  • Surely your use of `_index.html.erb` will be causing the error? I'm sure that won't load the `instance variables` you're setting. Can you post your partial code please? – Richard Peck May 04 '14 at 13:04
  • just have a look under my new post...: [link](http://stackoverflow.com/questions/23457042/rails-namespace-routes-and-controller) – oliver May 04 '14 at 13:51
  • <%= render 'manage/rooms/index'%> – oliver May 04 '14 at 15:15

0 Answers0