使用Rails 4.x访问模块资源

时间:2014-07-19 21:06:54

标签: ruby-on-rails ruby-on-rails-4 rails-routing

我正在使用带有Casein CMS的rails 4.1:https://github.com/russellquinn/casein

我在casein中设置了Post Model,view和controller,但我想访问casein以外的帖子,可能在另一条名为 blog

的路径下

我尝试过尝试重新修改路由和控制器,并列出了一系列错误。这里有人可能知道让这个工作的诀窍,并希望有些人可以帮助我,或者至少向我解释应该发生什么或者我可能做错了什么。

Casein添加到路线中的是:

#Casein routes
 namespace :casein do
    resources :posts
end

我想匹配索引并显示动作=>> /博客。我怎么能在我的routes.rb。

中正确写出来

我的控制器,我基本上从Casein的PostsController中提取了动作,并且包括Casein Module在内的所有帖子都试图简单列出。

以下是我的blogs_controller的索引操作:

class BlogsController < ApplicationController
module Casein
  def index
      @casein_page_title = 'Posts'
        @posts = Post.order(sort_order(:title)).paginate :page => params[:page]
    end

  end
end

最后我还想把博客带到博客,但我认为可以从那里开始,但如果有人有任何建议,那将非常感激。

1 个答案:

答案 0 :(得分:1)

您可能会要求这样做,但您的问题不是很明确。

如果您想拥有以下路线并为每个路线使用相同的控制器。

          Prefix Verb   URI Pattern                      Controller#Action
    casein_posts GET    /casein/posts(.:format)          casein/posts#index
                 POST   /casein/posts(.:format)          casein/posts#create
 new_casein_post GET    /casein/posts/new(.:format)      casein/posts#new
edit_casein_post GET    /casein/posts/:id/edit(.:format) casein/posts#edit
     casein_post GET    /casein/posts/:id(.:format)      casein/posts#show
                 PATCH  /casein/posts/:id(.:format)      casein/posts#update
                 PUT    /casein/posts/:id(.:format)      casein/posts#update
                 DELETE /casein/posts/:id(.:format)      casein/posts#destroy
            blog GET    /blog(.:format)                  casein/posts#index
                 GET    /blog/:id(.:format)              casein/posts#show

然后你的config / routes.rb文件应该包含

namespace :casein do
  resources :posts
end
get '/blog', to: 'casein/posts#index'
get '/blog/:id', to: 'casein/posts#show'

你需要你的控制器是app / controllers / casein / posts_controller.rb

但我非常强烈建议您使用2个不同的控制器,并关注共享方法