Ruby形式的帮助程序被较旧的值所吸引

时间:2018-04-13 20:11:08

标签: ruby-on-rails ruby ruby-on-rails-4 ruby-on-rails-5

在从CircleCreateService提交表单之前,我试图在值覆盖后获得正确的值时被阻止 从我的Circle创建服务我有这个:

def step_trigger(case_action)
  case case_action
    when 'User never create a circle'
      'ncreate'
    when 'User has created once'
      'onecreate'
    when 'User has created many'
      'mcreated'
  end
end

这就是我保存到数据库中的内容。所以我的问题是,从我的表单来看,当我尝试编辑Circle时,如何使用帮助器方法从数据库中获取旧值,以根据从我的数据库中检索到的数据呈现相应的选项?

这是我的辅助方法

def get_old_cirle_option(options)
  case options
    when 'ncreate'
      'User never create a circle'
    when 'oncecreate'
      'User has created once'
    when 'mcreated'
      'User has created many'
  end
end 

我的表格我用这个:

<%= select_tag :c_options, 
options_from_collection_for_select(
get_old_cirle_option(@circe.state)), 
class: "form-control" %>

它说:undefined method map'为nil:NilClass 你的意思是? tap`

1 个答案:

答案 0 :(得分:0)

options_from_collection_for_select需要4个参数:

  • collection,必须是数组
  • value_method
  • text_method
  • selected,这是可选的,如果省略则会获得nil

此外,get_old_cirle_option(@circe.state)会在您的案例中返回nil