[英]How to redirect from a new post url to the show post url in Buffalo
I am building a blog website in Buffalo, and I'm running into a bit of an issue. 我正在布法罗(Buffalo)建立博客网站,但遇到了一些问题。 I have the following routes in app.go
: 我在app.go
有以下路线:
b := BlogsResource{}
blogGroup := app.Group("/blog")
blogGroup.GET("/", b.List)
blogGroup.GET("/new", b.New)
blogGroup.GET("/post/{slug}", b.Show)
blogGroup.GET("/post/{slug}/edit", b.Edit)
blogGroup.POST("/", b.Create)
blogGroup.PUT("/post/{slug}", b.Update)
blogGroup.DELETE("/post/{slug}", b.Destroy)
blogGroup.Middleware.Skip(Authorize, b.List, b.Show)
And my blogs.go
resource create method looks like this: 我的blogs.go
资源创建方法如下所示:
func (v BlogsResource) Create(c buffalo.Context) error {
// Allocate an empty Blog
blog := &models.Blog{}
// Bind blog to the html form elements
if err := c.Bind(blog); err != nil {
return errors.WithStack(err)
}
// Get the DB connection from the context and validate it
tx := c.Value("tx").(*pop.Connection)
verrs, err := blog.Create(tx)
if err != nil {
return errors.WithStack(err)
}
if verrs.HasAny() {
// Make the errors available inside the html template
c.Set("errors", verrs)
// Render again the new.html template that the user can
// correct the input.
return c.Render(422, r.Auto(c, blog))
}
// If there are no errors set a success message
c.Flash().Add("success", T.Translate(c, "blog.created.success"))
// and redirect to the blogs index page
return c.Redirect(302, "blogPostPath()", render.Data{"slug": blog.Slug})
}
The new.html
looks like this: new.html
看起来像这样:
<div class="page-header">
<h1>New Blog</h1>
</div>
<%= form_for(blog, {action: blogPath(), method: "POST"}) { %>
<%= partial("blogs/form.html") %>
<a href="<%= blogPath() %>" class="btn btn-warning" data-confirm="Are you sure?">Cancel</a>
<% } %>
The problem I'm running into is when I try to do the redirect, it is pointing to the correct url localhost:3000/blog/post/my-blog-post-here
, but the body is trying to use the blogs/index.html
template instead of the blogs/show.html
. 我遇到的问题是当我尝试进行重定向时,它指向正确的URL localhost:3000/blog/post/my-blog-post-here
,但是主体尝试使用blogs/index.html
模板,而不是blogs/show.html
。 So, what do I need to do to have the redirect point to the correct URL and contain the right body? 那么,我需要怎么做才能使重定向点指向正确的URL并包含正确的正文? I've tried setting the <%= form_for(blog, {action: blogPath(), method: "POST"}) { %>
to <%= form_for(blog, {action: blogPostPath(), method: "POST"}) { %>
in the new.html
, but I get an error where I need the slug when going to localhost:3000/blog/new
. 我尝试将<%= form_for(blog, {action: blogPath(), method: "POST"}) { %>
为<%= form_for(blog, {action: blogPostPath(), method: "POST"}) { %>
在new.html
,但是在进入localhost:3000/blog/new
时,我遇到了需要new.html
的错误。
It looks like a routing issue. 看起来是路由问题。 I stumbled on this kind of issue. 我偶然发现了这种问题。 It was related to the order of the routes delcaration. 它与路线分配的顺序有关。 The router is order sensitive; 路由器是顺序敏感的; it will route to the first matching declaration... Maybe in your case the first route that matches is "/" I am not 100% sure, but try the other way round for ex : 它将路由到第一个匹配的声明...也许在您的情况下,匹配的第一个路由是“ /”,我不是100%肯定的,但是尝试用另外一种方法进行ex的转换:
blogGroup.GET("/post/{slug}", b.Show) blogGroup.GET(“ / post / {slug}”,b.Show)
blogGroup.GET("/post/{slug}/edit", b.Edit) blogGroup.GET(“ / post / {slug} / edit”,b.Edit)
blogGroup.GET("/", b.List) blogGroup.GET("/new", b.New) blogGroup.GET(“ /”,b.List)blogGroup.GET(“ / new”,b.New)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.