-1

I read many links specially on stackoverflow but could not find the solution as there was some difference . so I want to make my point clear as explained below:

I have an ajax call pointing to my controller and it has only one parameter google_searched_locations , which is basically contains json string

google_searched_locations      [{"geometry":{"location":{"J":31.482273,"M":74.33069999999998}},"icon":"https://maps.gstatic.com/mapfiles
/place_api/icons/restaurant-71.png","id":"b93a99a46343de01d0d928f99470f9b0f5f6f11d","name":"Dunkin' Donuts"
,"place_id":"ChIJSeoh6hkEGTkRsd0e1crAbHU","rating":4.3,"reference":"CnRhAAAA4x8yMjf9__CURWmYX6ojnpgu
-M1aL4Cvsmp6j2nKOLiqlBRlslTtPU8hUc6tJWAehCE967tW8Z623new_ivN8_PWbypr6ANDj_6AIxxGTQcwneyfHCigsHWhcdrUlcJAsQTycbHOTdmu6n8loZiU-hIQHNPqBNJRlho9fVjRfomU-BoUcdX_NGHhBFs_pxQiPZTlUD-W88o"
,"scope":"GOOGLE","types":["restaurant","food","point_of_interest","establishment"],"vicinity":"Lahore"
,"html_attributions":[]}]

my action contains the following code

 def searchResults

  @restaurant = GoogleSearchedLocation.new(params_google_searched_locations)
  byebug
  if @restaurant.save!
    render json: { :status => :Ok }  
  else
      render json: { :status => :failed }
  end
end

And code in params_google_searched_locations is as follows

def params_google_searched_locations
 params.require(:google_searched_locations).permit(:place_id)
 end

Now the whole ajax call fails throwing the following error in response

NoMethodError in GoogleSearchedLocationController#searchResults

undefined method `permit' for #< String:0x007f66ec5515b8 >

Solution with the reason will be more appreciated . Thanks in advance

Mani
  • 2,391
  • 5
  • 37
  • 81

1 Answers1

4

Usually, you would use a combination of permit/require on params where the parent returns a hash e.g. {user: {name: 'Maria'}.

However, the parent returns a JSON string. So what you would do is parse that JSON and then use the permit. However, keep in mind that you have just lost the indifference access from Rails where you can access a key using a symbol or a string

However, from your example since the JSON returns an array, I don't think you can use permit.

If it was a hash, I believe you could do

def params_google_searched_locations json = params.require(:google_searched_locations) {place_id: JSON.parse(json).permit(:place_id)} end

At any case, you don't necessarily need to use permit. You can whitelist the params yourself.

PericlesTheo
  • 2,429
  • 2
  • 19
  • 31
  • I highly doubt that is the same error. Parsing JSON wouldn't return a string so it is impossible to get the same error. Can you tell me what `JSON.parse(json)` returns? – PericlesTheo Oct 08 '15 at 12:57
  • and you said that i can white list parameters myself . can you explain that ? because as far i know we used to whitelist the params in models in rails 3 , and in rails 4 we use strong parameters . What should i do ? – Mani Oct 08 '15 at 12:57
  • The `permit/require` methods are just helpers, they are not really doing any magic. So for example in your case, you could do `params[:google_searched_locations][:place_id]` and it would have been exactly the same. The only differenence I think is that in production it would have raised on error to the client if the `google_searched_locations` param wasn't passed – PericlesTheo Oct 08 '15 at 13:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/91748/discussion-between-imrannaqvi-and-periclestheo). – Mani Oct 08 '15 at 13:03