我是Rails的新手,无法通过我创建的命名空间找到解决问题的方法。
问题是,如果我想在localhost:3000/manage/
下显示我的所有房间,它就不起作用。
错误:nil的未定义方法`title':NilClass。
如果我尝试在../manage/rooms/
下显示我的所有房间,它仍然没有显示我创建的所有房间错误:找不到ID ...如果我使用id,例如../manage/rooms/38
它正在发挥作用。希望可以有人帮帮我。
所以我们走了:
我创建了一个命名空间:
routes.rb中:
resources :manage
namespace :manage do
resources :rooms
end
耙路线:
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
在../ 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
,在... manage/rooms/_index.html.erb
下,它会在/layouts/_navigation_left.html.erb
谢谢!如果您需要更多信息,请询问。
谢谢对不起我是Rails的新手。如何更改控制器/路由,以便我不仅可以管理@rooms甚至@foo,@ _F ......感谢。
我尝试使用:
我想展示<%= @room.title %> <%= @room.number_of_rooms %>
或者您还需要其他东西吗?抱歉!
和<%= render 'manage/rooms/index'%>