【问题标题】:rails3-jquery-autocomplete reference id with formtasticrails3-jquery-autocomplete reference id with formtastic
【发布时间】:2013-06-18 17:57:41
【问题描述】:

我刚刚启动我的 Rails 应用程序,但遇到以下问题 - 我想使用 jQuery UI 和自动完成功能,因此使用了 gem rails3-jquery-autocomplete。我也将 formtastic 用于我的 rails 表单。

我有两个模型:

客户:

class Customer < ActiveRecord::Base
  has_many :carts
end

create_table :customer do |t|
  t.column :lastname, :string
  t.column :forename, :string
end

购物车:

class Cart < ActiveRecord::Base
  belongs_to :customer
end

create_table :cart do |t|
  t.column :costumer_id, :integer
end

如 gem 文档中所述,我在 CartController 中进行了自动完成配置:autocomplete :customer, :lastname, :full =&gt; true

在我看来,formtastic 代码看起来像这样:

<%= semantic_form_for @cart do |f| %>
    <%= f.inputs do %>
    <%= f.input :customer,
        :as => :autocomplete,
        :url => autocomplete_customer_lastname_carts_path,
        :id_element => '#cart_customer_id' %>

我的主要问题是,如何处理 id 的东西。我的意思是,我想将客户的参考 ID 存储在我的购物车模型中,而不是名称本身。我认识到可以使用:id_element 选项来引用一个id 输入字段,以便将id 存储在表单中。我发现,formtastic组合似乎有问题,并找到了潜在的solution。

我可以使用&lt;%= f.input :customer_id, :as =&gt; :hidden %&gt; 来做到这一点,但我真的不知道在该行中写什么:&lt;%= f.input :customer, :as =&gt; :autocom...。 :customer 是正确的解决方案吗?这种方法会给我以下信息:"cart"=&gt;{"customer"=&gt;"26","customer_id"=&gt;"0"}

有没有人有想法,可能我完全误解了整个 :id_element 的事情......

【问题讨论】:

    标签: ruby-on-rails jquery-ui formtastic


    【解决方案1】:

    您的迁移中的列名也有误。

    create_table :cart do |t|
      t.column :customer_id, :integer
    end
    

    好的,我找到了:)

    因此您需要使用 customer_id 创建一个 hidden_​​field 并告诉自动完成以更新此输入值。标签 :update_elements => {}

    <%= autocomplete_field_tag "cart_customer_name", "" , autocomplete_customer_lastname_carts_path, :id_element => "#customer_customer_id", :update_elements => {} %><br />
    <%= f.hidden_field :customer_id %>
    

    所以 :id_element 是选择结果时要更新的 id 元素。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-14
      • 1970-01-01
      • 2016-02-04
      • 2013-06-25
      • 2015-02-25
      相关资源
      最近更新 更多