Rails控制器中的Redirect_to不起作用,但服务器日志将页面显示为已呈现

时间:2011-12-06 12:29:19

标签: google-maps ruby-on-rails-3.1

我有一个应用程序(RoR 3.1),用户可以在其中更改企业的Google地图标记的位置(使用“业务控制器”处理)。点击“保存新位置”按钮后,用户应重定向到business#show页面。但他不是,虽然服务器日志显示业务#show已呈现。无论如何,该位置在数据库中正确保存,但我仍然留在业务#updatemap页面。

以下是用户可以更改地图位置的视图代码:

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?v=3.6&sensor=false&region=IN"></script>
<script type="text/javascript">
  function initialize() {

    var myLatlng = new google.maps.LatLng(<%= @business.latitude %>, <%= @business.longitude %>);

    var options = {
      zoom: 14,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }

    var map = new google.maps.Map(document.getElementById("map_canvas"), options);

    var marker = new google.maps.Marker({
        position: myLatlng,
        map: map,
        draggable:true
    });

    google.maps.event.addListener(
        marker,
        'drag',
        function() {
            document.getElementById('business_latitude').value = marker.position.lat();
            document.getElementById('business_longitude').value = marker.position.lng();
        });
  }
</script>

<body onload="initialize()">
<div id="map_canvas" style="width:500px;height:500px;">
</div>

<%= form_for :business, :url => { :action => "updatemap" }, :remote => true do |f| %>
  <%= f.hidden_field :latitude %>
  <%= f.hidden_field :longitude %>
  <%= f.submit %>
<% end %>

这是业务控制器的相关部分:

 def updatemap
    @business = Business.find(params[:id])
    if @business.update_attributes(params[:business])
      redirect_to @business, :flash => { :success => "The business was 
                                                      successfully updated!" }
    else
      redirect_to root_path
    end
  end

这是服务器日志:

Started GET "/businesses/173-New-Delhi-wrewerwer" for 127.0.0.1 at 2011-12-06 13:22:58 +0100
  Processing by BusinessesController#show as HTML
  Parameters: {"id"=>"173-New-Delhi-wrewerwer"}
  Business Load (0.3ms)  SELECT `businesses`.* FROM `businesses` WHERE `businesses`.`id` = 173 ORDER BY businesses.business_name ASC LIMIT 1
  City Load (0.1ms)  SELECT `cities`.* FROM `cities` WHERE `cities`.`id` = 2 LIMIT 1
  State Load (0.3ms)  SELECT `states`.* FROM `states` WHERE `states`.`id` = 10 ORDER BY states.name ASC LIMIT 1
  CACHE (0.0ms)  SELECT `cities`.* FROM `cities` WHERE `cities`.`id` = 2 LIMIT 1
  User Load (0.2ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
  CACHE (0.0ms)  SELECT `cities`.* FROM `cities` WHERE `cities`.`id` = 2 LIMIT 1
   (0.1ms)  SELECT COUNT(*) FROM `images` WHERE `images`.`business_id` = 173
  Image Load (0.6ms)  SELECT `images`.* FROM `images` WHERE `images`.`business_id` = 173
  CACHE (0.0ms)  SELECT `cities`.* FROM `cities` WHERE `cities`.`id` = 2 LIMIT 1
  CACHE (0.0ms)  SELECT `cities`.* FROM `cities` WHERE `cities`.`id` = 2 LIMIT 1
  CACHE (0.0ms)  SELECT `cities`.* FROM `cities` WHERE `cities`.`id` = 2 LIMIT 1
Rendered businesses/show.html.haml within layouts/application (49.9ms)
Rendered layouts/_stylesheets.html.erb (1.1ms)
Rendered layouts/_header.html.haml (3.3ms)
Rendered layouts/_footer.html.haml (1.1ms)
Completed 200 OK in 155ms (Views: 61.0ms | ActiveRecord: 8.1ms)

所以它表示已经渲染了business / show.html.haml,但我仍然在可以更改地图位置的页面上。

有谁知道这里发生了什么?我想这是一个非常简单的问题,但由于我对编码很陌生,所以欢迎任何帮助。

1 个答案:

答案 0 :(得分:0)

您需要从:remote => true中移除form才能进行重定向。

此致