不使用资源的注释的嵌套路由

时间:2015-07-29 21:07:45

标签: ruby-on-rails ruby multithreading ruby-on-rails-4 routes

我正在建立一个论坛,并且线程已经设置并且查看没有问题。在尝试在线程中创建注释的过程中,我似乎对如何设置路由感到困惑。通常我知道人们有嵌套路线:

resources :threads do
  resources :comments
end

虽然区别在于我的路线不使用资源方法,因为我单独设置它们:

get '/thread' => 'threads#discussion'
post '/create_thread' => 'threads#create'
get '/create_thread' => 'threads#new', :as => :new_thread
get '/threads/:id' => 'threads#show', :as => :thread_show
get 'threads/edit/:id' => 'threads#edit', :as => :edit_thread
put '/threads/edit/:id' => 'threads#update', :as => :update_thread
delete 'threads/:id' => 'threads#destroy'

线程/线程替换了实际的位置名称

我有办法在每个帖子中为评论放置嵌套路由吗?我为每个模型(用户,线程,注释)提供了正确的关联,并将user_id,thread_id添加到了注释模型。

如果您对正确设置评论路线或知道文章的方法有任何了解,请在此处列出。

非常感谢你!

修改

评论将直接放在主题页面上,而不是新页面。

1 个答案:

答案 0 :(得分:1)

您可以“手动”执行嵌套路由,如下所示:

get '/threads/:thread_id/comments/new' => 'comments#new'
get '/threads/:thread_id/comments/:id' => 'comments#show'

并且CommentsController的new操作可以执行以下操作:

def new
  @thread = Thread.find(params[:thread_id])
  @comment = @thread.comments.build
end

但是,我非常同意Jordan's advice above;你的路线有点乱,不完全RESTful,并且通过资源丰富的路由更容易表达。

相关问题