我有一个Rails控制器,它应该将我重定向到一个新创建的帖子:
class PostsController < ApplicationController
def index
end
def new
@post = Post.new
end
def create
@post = Post.new(params.require(:post).permit(:date, :rationale))
@post.save
redirect_to @post
end
def show
@post = Post.find(params[:id])
end
end显示方法的视图是:
<%= @post.inspect %>下列测试通过:
it 'can be created from new form' do
visit new_post_path
fill_in 'post[date]', with: Date.today
fill_in 'post[rationale]', with: "Some rationale"
click_on "Save"
expect(page).to have_content("Some rationale")
end但是,当我在浏览器中运行时,重定向只会转到索引/posts。
预期行为:用户应该重定向到视图显示并查看新创建的帖子
如果我硬编码id到重定向,我可以看到新创建的帖子。
发布于 2022-04-11 15:51:13
您可以使用这个查找路由,rails路由然后使用posts id查找路由,或者使用前缀/ URI模式。在这种情况下,你好像在用秀。然后您可以使用redirect_to '/post/: id /show‘来表示URI模式,id应该是@post.id
https://stackoverflow.com/questions/71830508
复制相似问题