1

I have parameters like this:

    Parameters: {
"map"=>[
{"lat"=>"51.088672", "lon"=>"71.396522", "vibration_level"=>"300", "time_sent"=>"07:25:00"}, 
{"lat"=>"51.088672", "lon"=>"71.396453", "vibration_level"=>"300", "time_sent"=>"07:25:01"}, 
{"lat"=>"51.088829", "lon"=>"71.396476", "vibration_level"=>"300", "time_sent"=>"07:25:14"}
]}

and strong params defined:

def map_params
  params.permit( map: [:lat,:lon, :vibration_level, :time_sent])
end

How to save all of my 3 objects in rails controller method. I can do that for 1 object but not for 3.

EDIT: for saving one object I use:

params.require(:map).permit(:lat, :lon, :vibration_level, :time_sent)

But, I guess this wouldn't work for multiple objects.

7
  • What's wrong with params.permit( map: [:lat,:lon, :vibration_level, :time_sent])? Commented Jun 14, 2015 at 18:28
  • @NitishParkar, params are permitted, I guess, but how to save them? does rails do it automatically if i call '@'map = Map.new(map_params); map.save Commented Jun 14, 2015 at 18:37
  • Do you want to save them all in a single record or do you want to create 3 map records? Commented Jun 14, 2015 at 18:50
  • @NitishParkar, 3 map records Commented Jun 14, 2015 at 18:51
  • Please post code of the controller action you are using to create records. Commented Jun 14, 2015 at 18:57

1 Answer 1

1

This is the basic way to create multiple record at a time with rails,

  maps = Map.create([
              {"lat"=>"51.088672", "lon"=>"71.396522", vibration_level"=>"300", "time_sent"=>"07:25:00"}, 
              {"lat"=>"51.088672", "lon"=>"71.396453", "vibration_level"=>"300", "time_sent"=>"07:25:01"},
              {"lat"=>"51.088829", "lon"=>"71.396476", "vibration_level"=>"300", "time_sent"=>"07:25:14"}
  ])

You can use your map_params for creating all maps at time as following

maps = Map.create(map_params["map"])
Sign up to request clarification or add additional context in comments.

10 Comments

Looks, like nothing happens. (0.1ms) begin transaction (0.1ms) rollback transaction
trace what is return by map_params, data format should be as in above example, if not re-write your def map_params.
sorry i am a new to rails, how can i trace?
past this puts map_params before Map.create(map_params), and observer development log.
Can you please print this puts map_params["map"], if it returns [{"lat"=>"51.088672", "lon"=>"71.396522", "vibration_level"=>"300", "time_sent"=>"07:25:00"}, {"lat"=>"51.088672", "lon"=>"71.396453", "vibration_level"=>"300", "time_sent"=>"07:25:01"}, {"lat"=>"51.088829", "lon"=>"71.396476", "vibration_level"=>"300", "time_sent"=>"07:25:14"}] then use Map.create(map_params["map"]), let me if works?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.