首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >未创建UGC

未创建UGC
EN

Stack Overflow用户
提问于 2011-10-15 14:48:51
回答 1查看 57关注 0票数 0

我正在尝试创建用户生成的帖子。我知道这些帖子是在数据库中创建的,但没有显示。终端puts:

代码语言:javascript
运行
复制
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'

说了同样的话,但我的应用程序给了我同样的错误:

代码语言:javascript
运行
复制
Couldn't find Event without an ID
app/controllers/events_controller.rb:22:in `show'

这是我的Events_Controller方法“show”的问题吗?

代码语言:javascript
运行
复制
def show
  @title  = "Your Events"
  @event  = Event.find(params[:id])
end

还是例行公事?我正在尝试显示创建的所有事件的索引。

提前感谢您的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-10-15 15:46:00

line 13上的EventsController中,您可以:

代码语言:javascript
运行
复制
redirect_to events_path

我相信这对应于上面日志中的第六行("Redirected to http://localhost:3000/events")。

但是,当您使用redirect_to时,它会发起一个新的GET请求,并且由于您没有指定任何参数,因此params为空。这就是为什么params[:id]nil,而Event.find(params[:id])抛出了你所看到的错误。

您确定不应该使用render :action => :showrender :action => :index而不使用redirect_to吗?与redirect_to不同,render不会发起新的请求,它只是在当前上下文中呈现指定的视图(在您的示例中,@event已经定义。

有关renderredirect_to的更多信息,请阅读Rails Guide on Layouts and Rendering,特别是第2节。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7776300

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档