Ruby on rails 重定向回不';我不再工作了
我有一些truble将我的用户重定向到上一页 下面是电影控制器中更新方法的示例Ruby on rails 重定向回不';我不再工作了,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我有一些truble将我的用户重定向到上一页 下面是电影控制器中更新方法的示例 def update @movie = Movie.find(params[:id]) if @movie.update_attributes(params[:movie]) flash[:notice] = "The given movie is now updated." end respond_with(@movie, location: :back) end 我得到了这个错误 使用行
def update
@movie = Movie.find(params[:id])
if @movie.update_attributes(params[:movie])
flash[:notice] = "The given movie is now updated."
end
respond_with(@movie, location: :back)
end
我得到了这个错误
使用
行上的对#进行未定义的方法“hash_for _back_url”
我正在使用Rails3.1RC1和Ruby1.9
它在做这样的事情时起作用
回复(@movie,location:request.referer)
有人知道为什么
:back
参数不起作用吗?在这种情况下,respond\u with应该是url助手方法的结果,如respond\u with(@movie,:location=>movie\u url(@movie))
您可能应该使用以下方法之一:
- 使用
重定向到信息页面,这相当于redirect\u to@movie
redirect\u to movie\u路径(@movie)
- 使用
(:edit表示用于显示表单的操作)使用更新的数据再次呈现表单render:action=>:edit
- 唯一对我有效的解决方案是使用
request.referer
解决方案#1
respond_with(@comment, location: request.referer)
解决方案#2
class ApplicationController < ActionController::Base
helper_method :redirect_back
def redirect_back(options = {})
if request.referer
redirect_to request.referer, options
else
redirect_to root_path, options
end
end
end
# View / controller
redirect_back notice: "Sending you back ... hopefully"
class ApplicationController
这个怎么样:
respond_with @movie do |format|
format.html { redirect_to :back }
end
允许您覆盖默认的html响应程序,以便您可以使用
重定向\u to:back
是否应该是:location=>:back?@jaydel-您可以在Ruby 1.9中使用新语法。可能值得尝试旧语法,以确保它与使用:location=>:back
时不一样。这是一个bug吗?我得到了一个未定义的方法'back\u url'。request.referer是新方法还是:back有问题?此解决方案依赖于MoviesController#show
方法,在我的案例中不存在此方法。问题是,我不知道用户是如何获得MoviesController#edit
页面的,但我仍然希望将用户重定向回原始页面。例如,我可以在我的站点上的10个不同位置拥有一个编辑链接。在编辑方法上保存引用,并通过会话或隐藏字段将其传递给更新方法。如果你需要@movie对象,那么在重定向到或回复中使用它。我仍然认为这应该可以自动工作。仅使用location::back
就足够了。response\u with不支持location::back。使用redirect_to location::back
代替。或者简化该帮助程序以返回路径并与respond_with:def redirect_back_path request.referer|root_path end一起使用