0

我有一个 Rails 3 博客风格的应用程序,其中有一个用于后端的管理命名空间和一个包含相应posts_controller.rb的controllers/admin子文件夹。

因此,页面的根 url 设置为“admin/posts#index”,并且帖子创建工作正常,除非我配置路由文件以将用户重定向到 root_url,如果他键入“/admin/articles”。

这是我的路线文件:

BlogDos::Application.routes.draw do
  # Index
  root to: "admin/posts#index"

  # If I uncomment these two lines below, the post#create function doesn't work. When I 
  # submit the "new post" form, the controller just skips the function entirelly and  
  # redirects me to admin/posts#index without creating the new post.
  # match "admin/posts" => redirect("/")
  # match "admin/posts/" => redirect("/")

  namespace :admin do
    resources :cpanel
    resources :posts do
      resources :comments, :only => [:create, :destroy]
    end
    root to: "cpanel#index"
  end
..
end

这是我的posts_controller.rb

def create
    @usuario = current_user
    @post = @usuario .posts.create(params[:post])

    respond_to do |format|
      if @post.save
        format.html { redirect_to article_dir_path(@post.year, @post.month, @post.slug), notice: 'Article was successfully created.' }
        format.json { render json: article_dir_path(@post.year, @post.month, @post.slug), status: :created, location: article_dir_path(@post.year, @post.month, @post.slug) }
      else
        format.html { render action: "new" }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

奇怪的是,这只发生在创建操作中,如果我编辑一篇文章并更新它,一切正常。我从 Rails 教程和 QA 网站上整理了几乎所有的东西,除了这个小问题,我确信它是相当简单的,但我是 Rails 的新手,还不太熟悉它的路由机制。

4

1 回答 1

0

创建帖子的表单提交给

/admin/posts

如果您将该路由重定向到索引页面,则永远不会调用控制器操作。

于 2013-04-22T17:10:22.917 回答