Rails渲染不刷新页面

时间:2013-08-27 18:04:42

标签: jquery ruby-on-rails ruby ajax ruby-on-rails-3

我正在研究ROR app。应用程序视图包含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

当前查看和根网址相同 - 投资组合/显示 现在视图上的按钮(仅限root_url)正在使业务逻辑正常,但页面没有刷新,而视图上的其他简单表单按钮正在执行此操作。按下按钮后,我在rails服务器中获取此信息:

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

任何猜测,但页面本身并不令人耳目一新。

2 个答案:

答案 0 :(得分:1)

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

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

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

像这样,

$.ajax({

 success: function(){
  window.location.reload();
 }
);

答案 1 :(得分:1)

不需要猜测。您的控制器响应错误的类型。

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

纠正它

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')
相关问题