我正在尝试创建用户生成的帖子。我知道这些帖子是在数据库中创建的,但没有显示。终端puts:
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
SQL (18.4ms) INSERT INTO "events" ("content", "created_at", "updated_at",
"user_id") VALUES (?, ?, ?, ?) [["content", "Test post."], ["created_at",
Sat, 15 Oct 2011 06:36:49 UTC +00:00], ["updated_at",
Sat, 15 Oct 2011 06:36:49 UTC +00:00], ["user_id", 1]]
Redirected to http://localhost:3000/events
Started GET "/events" for 127.0.0.1 at Sat Oct 15 00:36:49 -0600 2011
Processing by EventsController#show as HTML
Completed 404 Not Found in 1ms
ActiveRecord::RecordNotFound (Couldn't find Event without an ID):
app/controllers/events_controller.rb:22:in `show'说了同样的话,但我的应用程序给了我同样的错误:
Couldn't find Event without an ID
app/controllers/events_controller.rb:22:in `show'这是我的Events_Controller方法“show”的问题吗?
def show
@title = "Your Events"
@event = Event.find(params[:id])
end还是例行公事?我正在尝试显示创建的所有事件的索引。
提前感谢您的帮助。
发布于 2011-10-15 15:46:00
在line 13上的EventsController中,您可以:
redirect_to events_path我相信这对应于上面日志中的第六行("Redirected to http://localhost:3000/events")。
但是,当您使用redirect_to时,它会发起一个新的GET请求,并且由于您没有指定任何参数,因此params为空。这就是为什么params[:id]是nil,而Event.find(params[:id])抛出了你所看到的错误。
您确定不应该使用render :action => :show或render :action => :index而不使用redirect_to吗?与redirect_to不同,render不会发起新的请求,它只是在当前上下文中呈现指定的视图(在您的示例中,@event已经定义。
有关render与redirect_to的更多信息,请阅读Rails Guide on Layouts and Rendering,特别是第2节。
https://stackoverflow.com/questions/7776300
复制相似问题