Michael H.
Michael H.

Reputation: 481

How to access attributes of object sent to controller in rails

I have a trivial question but even after 2 days checking on stackoverflow I haven't found a solution.

When I forward the params to read a record from a database all works - but I found no way to access the parameters directly creating a view object or a columns array.

I receive following post request in my rails controller:

Started POST "/views/update" for 127.0.0.1 at 2017-08-07 17:27:51 +0400
Processing by ViewsController#update as HTML
  Parameters: {
    "view"=>{
      "id"=>1, 
      "name"=>"All Opportunties", 
      "columns"=>[
        {
          "id"=>1, 
          "label"=>"Name", 
          "fieldNameOrPath"=>"name", 
          "typ"=>"link", 
          "linkp"=>"id", 
          "link"=>"/opportunities/proposal/general", 
          "entity_id"=>1, 
          "hidden"=>true, 
          "created_at"=>"2017-08-06T22:12:03.000Z", 
          "updated_at"=>"2017-08-06T22:12:03.000Z"
        }, 
        {
          "id"=>2, 
          "label"=>"Account", 
          "fieldNameOrPath"=>"account.name", 
          "typ"=>"link", "linkp"=>"account_id", 
          "link"=>"/accounts", 
          "entity_id"=>1, 
          "hidden"=>false, 
          "created_at"=>"2017-08-06T22:12:03.000Z", 
          "updated_at"=>"2017-08-06T22:12:03.000Z"
        },
        {
          "id"=>3, 
          "label"=>"Owner", 
          "fieldNameOrPath"=>"user.username", 
          "typ"=>"string", 
          "linkp"=>nil, 
          "link"=>nil, 
          "entity_id"=>1, 
          "hidden"=>false, 
          "created_at"=>"2017-08-06T22:12:03.000Z", 
          "updated_at"=>"2017-08-06T22:12:03.000Z"
        },
        {
          "id"=>4, 
          "label"=>"Close Date", 
          "fieldNameOrPath"=>"closedate", 
          "typ"=>"datetime", 
          "linkp"=>nil, 
          "link"=>nil, 
          "entity_id"=>1, 
          "hidden"=>false, 
          "created_at"=>"2017-08-06T22:12:03.000Z", 
          "updated_at"=>"2017-08-06T22:12:03.000Z"
          }, 
          {
            "id"=>5, 
            "label"=>"Stage", 
            "fieldNameOrPath"=>"stage", 
            "typ"=>"string", 
            "linkp"=>nil, 
            "link"=>nil, 
            "entity_id"=>1, 
            "hidden"=>false, 
            "created_at"=>"2017-08-06T22:12:03.000Z", 
            "updated_at"=>"2017-08-06T22:12:03.000Z"
          }
        ]
      }
    }

Ideally I want to have the received parameter in an object called view from where I can access its attributes - specially the columns array. My object structure is:

View { id, name, columns [{id, label ...}] }

I want to loop through all columns:

view.columns.each do |column|
  column.name = ...
end

It looks to me very basic but all methods I tried to assign the params to view (or create an object for columns only) failed. Most time I get the error message method xxx of view unknown.

Thanks for helping!

Upvotes: 1

Views: 4013

Answers (3)

Gerry
Gerry

Reputation: 10507

Try this:

params['view']['columns'].each do |column|
  puts column['id']
end

That will print in console the id of each column:

1
2
3
4
5

Upvotes: 1

Sal Ortiz
Sal Ortiz

Reputation: 1

the parameters would be accessed via the params object as a Hash:

params['view']['id']    # 1
params['view']['name']  # All Opportunities
params['view']['columns'].first['typ']    # link

However, it sounds like all that you need is more like a wrapper object for the parameters in 'view'. I don't know of any specifics, offhand, for your data, but you can create simple objects, based on hash data, using OpenStruct objects.

As for the actual work of creating said object so that it is accessible in the controller, consider using a :before_filter in your Rails Controller to create an object using the data in params as input.

Upvotes: 0

David Hempy
David Hempy

Reputation: 6287

Something like this should work:

view = params[:view]

view[:columns].each do |column|
  column[:name] = ...
end

That said, this feels like code that might better fit in a model than the controller.

Upvotes: 0

Related Questions