gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 16:49:05 25 4
gpt4 key购买 nike

@tech_data = TagNode.first 从 Controller 输出以下对象。

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 中的 attr_accessor 这样的东西,What is attr_accessor in Ruby?请在您的模型中包含以下内容,这将使从表单到 Controller 获取值。属性访问器:类别

关于ruby-on-rails - 如何在 rails simple_form_for 中添加自定义名称输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27019689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com