I have an application (RoR 3.1) where a user can change the position of a google maps marker of a business (handled with “business controller”). After he clicks on the “Save new position” button the user should be redirected to the business#show page. But he isn’t although the server log shows that business#show is rendered. The position is saved correctly in the database anyhow but I’m remaining on the business#updatemap page.
Here is the code of the view where the user can change the map position:
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?v=3.6&sensor=false®ion=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 %>
This is the relevant part of the business controller:
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
And this is the server log:
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)
So it says that business/show.html.haml was rendered, but I’m still on the page where I can change the position of the map.
Has anyone an idea of what is going on here? I guess this is a really simple issue but since I’m quite new to coding any help is welcome.
You need to remove
:remote => truefrom yourformin order to make redirection works.Regards