Rails 日志显示重定向但视图保持不变

Rails log show redirect but view remain same

我正在使用 rails 3.2.21。我想在特定条件下创建新元素后重定向请求。 我的控制器创建操作代码:

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

结束

N.B:我没用 远程:正确 用于表单提交。

我在日志中看到它将请求重定向到正确的路径并呈现正确的页面。我使用 Rails.logger.info 检查我预期的页面在哪里呈现,在我看来没问题。 我的创建操作日志信息:

Started POST "/community/groups" for 127.0.0.1 at 2015-03-03 14:18:47 +0600
Processing by Community::GroupsController#create as HTML 

然后成功重定向以显示操作(我的日志信息)

Redirected to http://localhost:3001/community/groups/53
Completed 302 Found in 1008.4ms (ActiveRecord: 208.7ms)

现在开始新的请求:(我的日志信息)

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。我不明白这个问题。如果有人遇到相同类型的问题,请帮助。

我的表演代码:

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

结束

我解决了我的问题。这不是控制器级别的问题或请求重定向相关的问题(根据代码请求重定向)。我的问题是,我使用 iframe 作为我的表单目标。因此,请求重定向到目标 iframe 并刷新它,但我期待整页刷新。我使用JavaScript强制刷新整页来解决我的问题。

window.location.reload();

534 other ways to reload the page with JavaScript.

对于像我这样的网络开发新手,我想添加有关 html 表单目标属性的额外信息。

Target 用于指定 window 您希望在提交表单时显示服务器的响应。

可能的值为:

  • _blank - 新页面
  • frame - 在 iframe 中显示给定名称
  • _self - 在表单所在的同一个 iframe 中展示
  • _parent - 在表单的 iframe
  • 的父级 page/iframe 中显示
  • _top - 最顶层 window

我认为以上信息可能对某人有价值。