Rails log show redirect but view remain same
我正在使用Rails 3.2.21。我想在特定条件下创建新元素后重定向请求。
我的控制器创建操作代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | def create @community_group = Community::Group.new(params[:community_group]) @community_group.member_ids = [] @community_group.member_ids = params[:member_ids].present? ? [@current_user.id] + params[:member_ids] : [@current_user.id] respond_to do |format| if @community_group.save format.html{ redirect_to community_group_path(@community_group) } else flash.now[:error] = 'Group Saving Failed' format.html{} end end |
end
N.B:我没有使用
远程:真
用于表单提交。
我在日志中看到它将请求重定向到正确的路径并呈现正确的页面。我使用Rails.logger.info进行检查,在哪里显示了我期望的页面,在我看来还可以。
我用于创建操作的日志信息:
1 2 | Started POST"/community/groups" for 127.0.0.1 at 2015-03-03 14:18:47 +0600 Processing by Community::GroupsController#create as HTML |
,然后成功重定向以显示操作(我的日志信息)
1 2 | Redirected to http://localhost:3001/community/groups/53 Completed 302 Found in 1008.4ms (ActiveRecord: 208.7ms) |
现在开始新的请求:(我的日志信息)
1 2 3 | Started GET"/community/groups/53" for 127.0.0.1 at 2015-03-03 17:08:52 +0600 Processing by Community::GroupsController#show as HTML Parameters: {"id"=>"53"} |
但我的视图仍在同一页面上,但具有相同的URL。我不明白这个问题。如果有人遇到相同类型的问题,请提供帮助。
我的表演动作代码:
1 2 3 4 5 6 | def show @community_group = Community::Group.find(params[:id]) @community_group_post = @community_group.community_group_posts.order("id DESC").page(params[:page]).per_page(10) respond_to do |format| format.html end |
end
我解决了我的问题。这不是控制器级别的问题,也不是请求重定向相关的问题(根据代码请求重定向)。我的问题是,我将iframe用作表单目标。因此,请求重定向到目标iframe并刷新它,但是我希望刷新整个页面。我使用JavaScript强制刷新整个页面来解决我的问题。
1 | window.location.reload(); |
534种其他使用JavaScript重新加载页面的方法。
对于像我这样的新手Web开发人员,我想添加有关html表单目标属性的额外信息。
目标用于指定提交表单后要在哪个窗口中显示服务器的响应。
可能的值为:
- _blank-新页面
- 框架-在iframe中以给定名称显示
- _self-在表单所在的同一iframe中显示
- _parent-在表单的iframe的父页面/ iframe中显示
- _top-最上面的窗口
我认为上述信息可能对某人有价值。