Mario
Mario

Reputation: 1253

Managing Action Controller Parameters

I have this parameters:

 Parameters: {
"diagram"=>{"name"=>"name123"}, 
"isit"=>{
"0"=>{"xposition"=>"171", "yposition"=>"451", "titleid"=>"isit0", "description"=>"-description-", "leftrelationsids"=>"", "rightrelationsids"=>""}, 
"1"=>{"xposition"=>"254", "yposition"=>"554", "titleid"=>"isit1", "description"=>"-description-", "leftrelationsids"=>"", "rightrelationsids"=>""}}}

In the create method that receives the parameters above I want to store a diagram (that for now it it just the name of it) and after that I want to store each of the components.

I'm doing this in the diagrams_controller.rbcreate method. The diagram has_many components.

My problem is how to store the components data?

I have tried this (for now just tried to of the columns, xposition and yposition):

def create
  @diagram = Diagram.new(diagram_params)
  @diagram.save

  @diagram.components.create(params.require(:isit).permit(:xposition, :yposition))

The diagram are stored, but the components not. I don't know how to handle this require permite thing to the components.

Here is the result:

enter image description here

Any help? How should I store the components?

Upvotes: 0

Views: 49

Answers (1)

Mohamed Yakout
Mohamed Yakout

Reputation: 3036

Try to use this code:

@diagram = Diagram.new(diagram_params)
@diagram.save
component = Component.create(params.require(:isit).permit(:xposition, :yposition))
@diagram.components << component
@diagram.save

Or use accepts_nested_attributes_for in diagram model, and edit diagram_params method to add the following:

params.require(:diagram).permit(components_attributes: [:xposition, :yposition])

Read about accepts_nested_attributes_for

Upvotes: 1

Related Questions