0

我正在开发 ROR 应用程序。应用程序视图包含 ajax 和 jquery 代码:

jQuery.ajax({
  data:     'val=' + val,
  dataType: 'script',
  type:     'post',
  url:      "/portfolio/update"
});

更新操作包含以下代码:

def update
  j = params[:val]

  # Buisness logic code
  redirect_to root_url, notice: "update done"
end

当前视图和根 url 相同 - 投资组合/显示 现在视图上的按钮(仅 root_url)正在执行业务逻辑,但页面没有刷新,而视图上的其他简单表单按钮正在执行此操作。按下按钮后,我在 Rails 服务器中得到了这个:

Rendered portfolio/show.erb (1.5ms)
Completed 200 OK in 24ms (Views: 4.1ms)

任何猜测,但页面本身并不刷新。

4

2 回答 2

1

无需猜测。您的控制器以错误的类型响应。

redirect_to只能在 html 响应下工作。当您的请求类型为 'js' 的 'script' 时,将无法呈现任何内容。

纠正它

def update
  # blah blah
  respond_to |format|
    format.html { redirect_to some_path }
    format.js # This will render default js template views/portfolio/update.js.erb
  end
end

然后准备js模板

$('#div_to_update').html('new html code to update the view')
于 2013-08-27T18:50:32.023 回答
1

Ajax 用于提供不需要刷新页面的功能。如果你想刷新页面,那么你应该使用 html 表单和提交按钮。

  <%= form_for %>
   .....
  <% submit %>

或者使用 Ajax,您需要在成功回调时手动执行,

像这样,

$.ajax({

 success: function(){
  window.location.reload();
 }
);
于 2013-08-27T18:10:14.783 回答