Ruby on rails 重定向到类别帖子路径(@post)
我有帖子,还有帖子类别 职位所属:类别 类别有很多:帖子 我希望在创建新帖子时,rails将我重定向到已创建的帖子 但是当我在Posts控制器中使用Ruby on rails 重定向到类别帖子路径(@post),ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,我有帖子,还有帖子类别 职位所属:类别 类别有很多:帖子 我希望在创建新帖子时,rails将我重定向到已创建的帖子 但是当我在Posts控制器中使用 def create @category = Category.find(params[:category_id]) @post = current_user.posts.build(post_params) if @post.save flash[:success] = "Поздра
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
这个
rails给了我错误
没有路由匹配{:action=>“show”、:controller=>“posts”、:category_id=>#、:id=>nil、:format=>nil}缺少必需的键:[:id]
但是我想要if@post.save rails重定向到created post
请帮忙。我打赌你的路线是这样的:
resources :categories do
resources :posts
end
这将创建URL帮助器类别\u post\u路径,但它需要一个类别和一个post
试试这个:
category_post_path @category, @post
很抱歉,现在rails在noob中给出错误参数PostsController中出现错误参数数(0为1)def show@posts=Post.find(params(:id))您的问题是
params(:id)
应该是params[:id]
。请注意方括号。全文:@posts=Post.find(params[:id])
。如果需要解释:params
是一个返回散列的方法,因此params[:id]
调用params方法,返回散列,然后检索index:id
的值。
category_post_path @category, @post