Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
203 views
in Technique[技术] by (71.8m points)

undefined method `permit' for #<String:0x007f66ec6ff180> ruby on rails

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...