【发布时间】:2011-12-02 15:19:44
【问题描述】:
我还没有弄清楚路由的机制..
我有一个用户、消息和评论模型。
用户创建了一个消息,我想在消息中为 cmets 放置简单的文本框。类似于推特。
但是,当表单被提交(使用下面的表单)而不是返回到 localhost/msgs/:id 时,它返回到 localhost/cmets。
我对 /cmets 没有看法,我也不想看到。我希望所有 cmets 都显示在 msg/:id 页面中。
cmets 控制器:
class CommentsController < ApplicationController
before_filter :authenticate
def create
msgid = flash[:msg]
@current_msg = Msg.find(discid)
@comm = @current_msg.comments.build(params[:comment])
@comm.user_id = current_user.id
@comm.msg_id = msgid
puts discid
if @comm.save
flash[:success] = "Comment posted"
redirect_to msg_path(discid)
else
flash[:error] = "Comment was not posted."
redirect_to msg_path(discid)
end
end
路由.rb
match '/comments' , :to => 'msgs#show'
resources :users
resources :msgs
因为 cmets 显示在 msgs 的显示视图中,所以这里是 msgs 控制器中的显示操作
def show
@msg= Msg.find(params[:id])
@title = @msg.title
@comment = Comment.new
@comments = @msg.comments.all
flash[:msg] = @msg.id
end
我得到的错误是
ActiveRecord::RecordNotFound in MsgsController#show
Couldn't find Msgwithout an ID
它指向第 46 行,目前是 @msg = Msg.find(params[:id])
如果我删除路线并放置一个常规的resources :comments,我会得到一个缺少的 cmets/create 模板..
感谢您的帮助。
【问题讨论】:
-
问题已解决.. 我在 redirec_to 中添加了一个 :id => discid.. 还使用了stackoverflow.com/questions/6495046/template-is-missing 来理解问题
-
请将其作为答案并将其标记为已接受,以便其他人知道此问题已解决