48248

undefined method `permit' for # ruby on rails

Question:

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

<blockquote>

NoMethodError in GoogleSearchedLocationController#searchResults

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

</blockquote>

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

Answer1:

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.

Recommend

  • Are context.startService() calls guaranteed to be aquired by the service in the same order they were
  • get Lat Lang from a place_id returned by autocomplete place api
  • Maximum in group by active record query
  • How to find TMax without using shifts
  • Get Common Rows Within The Same Table
  • How to just read wanted data from JSON in Java & Android
  • Android: Notify user of a newer version
  • useState set method not reflecting change immediately
  • Jackson JSON generates HTTP status 500, XML works
  • MySQL Rating System - Find Rating
  • S4s-elt-invalid-content.1: Element 'element' Is Invalid, Misplaced, Or Occurs Too Often
  • jQuery Not allowed to load local resource
  • Forcing a 1e3 instead of 1000 format in ggplot R
  • Google Places JS API Show Reviews
  • Linked List From Text File
  • Spark MLlib example, NoSuchMethodError: org.apache.spark.sql.SQLContext.createDataFrame()
  • How to get place or placeid by latLng in android using google places api?
  • How to change the Column Names of a database created by Django (from models.py)?
  • Shiny semantic Ui elements in renderUI
  • How to sort children by value in jQuery
  • Uncaught TypeError: Cannot call method 'insert' of undefined
  • Jquery is not working on newly added elements to the dom with Plugin like JRate Plugin
  • Android Cursor Problem
  • python statistic top 10
  • Plotly - Create Pie Chart with continuous color scale
  • Mysql optimization based on explain
  • DataFrame transformation in Spark, Java
  • How do I keep the legends/colours of SSRS charts consistent?
  • how to make NSManagedObjectContext dirty (hasChanges = YES) Manually
  • Is it good practice to put Edge Side Includes into my templates?
  • Cannot establish BLE connection between Android 5 and BLE device
  • How can i get window.location.pathname on my test file using Jest?
  • Fail SonarQube quality gate when coverage decreases
  • How to set the navigation bar to the top in Table View?
  • Partial specialization of a class template in derived class affects base class
  • c++ regex_replace not doing intended substitution
  • using System.Speech.Synthesis with Windows10 universal app (XAML-C#)
  • GAE: Way to get reference to an HttpSession from its ID?
  • Spring boot 2.0.0.M4 required a bean named 'entityManagerFactory' that could not be found
  • What is the purpose of TaskExecutor in spring?