Anish
Anish

Reputation: 558

How to pass nested key to the object of rails params

i am generating RFP plugin in my project management tool .In that plugIn i am basically filling one form has few usr requirements like which technology how many resources ,hours etc....

Now when submmitting the form after filling it , suppose i fill 5 fields in a form (may be text field,text area ,boolean etc...)then submmit it the 5 issues would be created

ie 1st field of the form should create 1st issue

2nd field of the form should create 2nd issue

3rd field if i keep blank in a form then that issue wont't created

and so on

i want to create new issue for each hash key if value is present for perticular key

one solution is that in user hash there are many key & values come after filling a form but how could in separate each key value and based on that applying some looping and create new issues

My params[:user1] is

    {"project_id"=>"first1",
    "user1"=>
     {"ffval_attributes"=>{"ckval"=>"0"},
     "opval_attributes"=>{"ckval"=>"0"},
     "jvusedfor"=>"menu sidebar",
     "user3_attributes"=>
     {"drupal"=>"0",
     "wordpress"=>"0",
     "joomla"=>"0",
     "other2"=>"0",
     "Typo3"=>"0"},
     "user2"=>
       {"cms_ex"=>"ruby", "java"=>"0", "dotnet"=>"0", "other1"=>"1", "php"=>"0"},
       "pagenumber_attributes"=>{"uniquepages"=>"3", "subsequentpages"=>"4"},
       "gcvals_attributes"=>{"ckval"=>"0"},
       "pagetype"=>"false",
       "nojavascript"=>"7",
       "src_id"=>"",
       "numberofmenu"=>"5",
       "ss1"=>"0",
       "type1"=>"false",
       "sfval_attributes"=>{"ckval"=>"0"},
       "design_pattern"=>"5",
       "dcomplex"=>"no",
       "ieval_attributes"=>{"ckval"=>"0"},
       "checkpixel"=>"true",
       "base"=>"true",
       "nohacks"=>"5",
       "fuvalue"=>"true",
       "mobile"=>"true"},
       "utf8"=>"",
       "commit"=>"Create User1",
       "authenticity_token"=>"UlrGhESPKK6zSr6aL2iIpGt9mR2K5tgBA3NnZP44+XE="}

i tried in controller

          u = params[:user1]
      a = Issue.last
      a = a.id

      u.each do |key, value|

     if   value.present?

    issue = Issue.new
    issue.id = a + 1 
    **issue.subject = key**
    ssue.tracker_id = 1
    issue.project_id = 1
    issue.priority_id = 2
    issue.status_id = 1
    issue.author_id = 1
    issue.save
    a = a + 1
    end 

My question is

How to pass nested hash key on issue.subject ,if their value is present Thanks in advance

Upvotes: 0

Views: 808

Answers (2)

Amit Thawait
Amit Thawait

Reputation: 5292

If you want to create Issue objects based on user parameters that are coming from params then,

first select only user params

user_params = params.select{|k,v| k.include?('user')}

pass selected user_params to a method,

def get_issue_attribute(user_params)
  user_params.each do |key, value|
    value.is_a?(Hash) ? get_issue_attribute(value) : create_issue({key => value})
  end
end

recursive call is used in the above method to get the inner-most params of user_params

def create_issue(attr) 
  # Code for issue creation
end

Upvotes: 1

Betjamin Richards
Betjamin Richards

Reputation: 941

You can use has_key? function to confirm whether a value is present in the hash and then if so process the data.

Alternatively you could do something like this to check whether a value exists inside your nested hash:

if params[:issue]
   if params[:issue][:subject]
        # whatever code you want to do
   end
end

Upvotes: 0

Related Questions