【问题标题】:How to add custom name input field in rails simple_form_for如何在 rails simple_form_for 中添加自定义名称输入字段
【发布时间】:2015-01-17 03:20:22
【问题描述】:

@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 值而不将此字段添加到模型中?

【问题讨论】:

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


    【解决方案1】:

    你需要像 Ruby 中的 attr_accessor 这样的东西,What is attr_accessor in Ruby? 请在您的模型中包含以下内容,这将使从表单获取值到控制器。 attr_accessor : 类别

    【讨论】:

    • 马诺吉。他的帖子写道,他不想在模态中include/modify 任何东西
    • 他的意思是,他不想将字段添加到表格中。
    • Manoj 是对的,attr_accessor 只是表示属性可以由模型获取/设置,与数据库无关..
    • @user3636388 对您有帮助吗?
    猜你喜欢
    • 1970-01-01
    • 2011-03-13
    • 1970-01-01
    • 1970-01-01
    • 2016-07-17
    • 1970-01-01
    • 2014-04-07
    • 1970-01-01
    • 2012-06-23
    相关资源
    最近更新 更多