我需要responder respond_with的帮助,因为当我创建一个新的帖子(在这种情况下)它不会重定向到指定的位置。可能是什么?这是我的一段代码,创建了一个新的帖子,但它被重定向到create.html.erb,而不是我指定的index.html.erb。
def create
@post = Post.create(params[:post])
respond_with(@post, :status => :created, :location => posts_url)
end
尝试使用"redirect_to"(来自Rails 3中的ACIIcasts - Controllers):
redirect_to @result, :notice => "Successfully converted original data."
如果你对这个解决方案不满意,我在一个类似的帖子中找到了一个解决方案:链接
def convert
@result = Result.find(params[:id])
@result.convert_original_data
success = @result.save
respond_with(@result) do |format|
format.html {redirect_to result_path(@result) } if success
end
end
你不需要提供位置。它会自动为你做。
你所需要做的就是…
respond_with @post
它将设置正确的状态并将您重定向到posts_url。