0

我在我的项目管理工具中生成 RFP 插件。在那个插件中,我基本上填写了一个表格,几乎没有 usr 要求,比如哪种技术有多少资源、小时数等。

现在在填写后提交表单时,假设我在表单中填写了 5 个字段(可能是文本字段、文本区域、布尔值等......)然后提交它,将创建 5 个问题

即表单的第一个字段应该创建第一个问题

表格的第二个字段应该创建第二个问题

第三个字段如果我在表单中保持空白,则不会创建该问题

等等

如果特定键存在值,我想为每个哈希键创建新问题

一种解决方案是,在用户哈希中,填写表格后有许多键和值,但是如何将每个键值分开并在此基础上应用一些循环并创建新问题

我的 params[:user1] 是

    {"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="}

我试过控制器

          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 

我的问题是

如何在 issue.subject 上传递嵌套哈希键,如果它们的值存在提前谢谢

4

2 回答 2

1

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
于 2013-10-11T16:07:43.717 回答
0

你可以使用has_key吗?函数来确认一个值是否存在于散列中,然后如果存在则处理数据。

或者,您可以执行以下操作来检查嵌套哈希中是否存在值:

if params[:issue]
   if params[:issue][:subject]
        # whatever code you want to do
   end
end
于 2013-10-11T12:34:16.410 回答