0

My chap_three_controller.rb file :

class ChapThreeController < ApplicationController
   def create
      @marker = Marker.new(params[:m])
      if @marker.save
         res={:success=>true,:content=>"<div><strong>found </strong>#{marker.found}
              </div><div><strong>left </strong>#{marker.left}</div>"}
      else
         res={:success=>false,:content=>"Could not save the marker"}
      end
      render :text=>res.to_json
   end
end

My routes.rb file

match '/map3', :to => 'chap_three#map'
match '/map3/create', :to => 'chap_three#:action'

Am I doing it right matching the create function in the controller to my routes? Because it isn't working..

This is a snippet of my javascript code:

request.open('GET', 'create' + getVars, true);
 request.onreadystatechange = function() {
    if (request.readyState == 4) {
 //the request is complete
    var success=false;
    var content='Error contacting web service';
    try {
 //parse the result to JSON (simply by eval-ing it)
      res=eval( "(" + request.responseText + ")" );
      content=res.content;
      success=res.success;
    }catch (e){
       success=false;
     }

The error I keep getting is 'Error contacting web service'. This means that my request.responseText isn't working and the create method in my controller isn't doing anything....Any help would be great

sushmit sarmah
  • 7,508
  • 6
  • 21
  • 24

1 Answers1

0

You want your match to be:

match '/map3/create' => 'chap_three#create'

The # separates controller name and action name respectively and the :to is unnecessary and only used when defining a root route. I recommend reading up on the Rails Routing Guide

Marc Baumbach
  • 10,323
  • 2
  • 30
  • 45
  • Trying to store params[:m] to database through @marker = Marker.new(params[:m])... but its not working..is it a mass assignment problem? cant figure it out – sushmit sarmah Feb 08 '13 at 05:49
  • That's fine, any query parameters you add to the URL will be available in `params`. So if you're URL was `/map3/create?m[left]=some_value&m[found]=true` then `params[:m]` would have values for `left` and `found`. – Marc Baumbach Feb 08 '13 at 05:50
  • It could be a mass assignment problem. Make sure you have `attr_accessible` for the values you are sending. You can also check if `@marker.save` is returning `false` and if so, check `@marker.errors` to see why it's failing. You can also switch it over to `@marker.save!` which will throw an exception and might make it more obvious during your debugging. – Marc Baumbach Feb 08 '13 at 05:51
  • I checked the server output and the values are according to what you have said and m holds all values as a hash.... but it isn't getting stored in the database Marker... Any idea why that could be happening? It shows Active_Record:0.0ms – sushmit sarmah Feb 08 '13 at 05:55