我需要响应者 respond_with 的帮助,因为当我创建一个新帖子时(在这种情况下)它不会重定向到指定的位置。可以是什么?这是我创建新帖子的代码,但它被重定向到 create.html.erb 而不是我指定的 index.html.erb。
def create
@post = Post.create(params[:post])
respond_with(@post, :status => :created, :location => posts_url)
end
我需要响应者 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”(来自ACIIcasts - Rails 3 中的控制器):
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。