【问题标题】:Bad routing, rails路由错误,导轨
【发布时间】: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 模板..

感谢您的帮助。

【问题讨论】:

标签: ruby-on-rails-3 routes


【解决方案1】:

问题已解决。我在redirecא_to 中添加了:id =&gt; discid

也使用Template is missing 了解问题

【讨论】:

    猜你喜欢
    • 2018-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多