2013-04-22 48 views
0

我有一个Rails 3博客风格的应用程序,在那里我有一个管理命名空间后端的目的和所含有的相应posts_controller.rb一个控制器/管理子文件夹。的Rails 3.2重定向跳过创建行动

因此,页面的根网址设置为“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很陌生,对它的路由机制还不是很熟悉。

回答

0

创建后提交给

/admin/posts 

如果您重定向到索引页航线的形式,控制器动作永远不会被调用。

+0

我看到,是否有任何解决方法来解决这个问题,而无需完全删除重定向功能? 谢谢! – 2013-04-22 17:15:32

+0

你为什么要重定向? – AlexBrand 2013-04-22 17:21:37

+0

我不希望该网站拥有/ admin/posts路径可访问,我只是希望它是根 – 2013-04-22 17:24:28