如何在rails simple_form_for中添加自定义名称输入字段

时间:2014-11-19 14:46:07

标签: ruby-on-rails ruby haml simple-form

@tech_data = TagNode.first输出来自控制器的对象。

p @tech_data
#<TagNode _id: 5119dcf74054448e576f3392, parent_id: nil, parent_ids: [], name: "categories", path: "categories", _type: "TagNode">

我的表单包含name_typecategory字段。但是我的对象没有字段category

这是我的表格

  = simple_form_for @tech_data, as: :techtags, :url => view_techtags_technologies_path, :remote => true,:method => :get,:html => {:id => "upsert_techtags",:data => {:spinner => "#tech-ui"}} do |f|
  %label.pull-left Type
  = f.input :_type, :collection => tech_types, :label => false, :input_html => {:class =>"chzn-select", :data => {:placeholder => "Select Technology Type"}, :multiple => false}
  %label.pull-left Name
  = f.input :name, :type=>"text", :required => true, :label => false, :placeholder =>"Type the New Technology", :input_html => {:style => "width:265px;margin-bottom:0;"}
  %label.pull-left Category
  = f.input :category, :collection => [1,2,3,4], :label => false, :input_html => {:class =>"chzn-select", :disabled=>"true",:data => {:placeholder => "Type or select category"}, :multiple => false}
  = f.submit "Create", :class => "btn btn-primary pull-right"

我的表单还有一个我的对象没有的附加字段。加载表单时出错,因为我的对象中没有category字段。

如何在不将此字段添加到模型的情况下选择表单中的category值?

1 个答案:

答案 0 :(得分:0)

你需要像Ruby中的attr_accessor,What is attr_accessor in Ruby? 请在您的模型中包含以下内容,以便从表单到控制器获取值。 attr_accessor:category