redirect_to category_post_path(@post)

时间:2013-11-08 15:36:54

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

我有帖子和帖子类别 发布belongs_to:类别 类别has_many:帖子 我想在创建新的帖子后将我重定向到创建的帖子 但是当我在Posts控制器中使用时

def create
        @category = Category.find(params[:category_id])
        @post =  current_user.posts.build(post_params)
        if @post.save
      flash[:success] = "Поздравляем Ваше задание опубликованно"
      redirect_to category_post_path(@post) 
  else
    render 'new'
  end
    end

redirect_to category_post_path(@post)

rails给我错误

没有路由匹配{:action =>“show”,:controller =>“posts”,:category_id =>#,:id => nil,:format => nil}缺少必需的密钥: [:ID]

但我想要@ post.save rails redirect_to创建帖子

请帮助。

1 个答案:

答案 0 :(得分:1)

我打赌你的路线看起来像这样:

resources :categories do
  resources :posts
end

这会创建URL帮助程序category_post_path,但它需要一个类别和一个帖子。

试试这个:

category_post_path @category, @post
相关问题