English 中文(简体)
铁路简单窗体组合组合(_选择异常行为)
原标题:Rails simple_form grouped_select strange behavior

我最近发生了一件奇怪的事

我有两种模型:

class Direction < ActiveRecord::Base
  has_many :specializations, dependent: :destroy
end

and

class Specialization < ActiveRecord::Base
  belongs_to :direction
end

它们都有一个称为 标题 的字段。

我用一组选择制成表格( 与任何模型无关) :

= simple_form_for :some_name do |f|
  = f.input :specialization_id, collection: Direction.all,
            as: :grouped_select,
            group_method: :specializations

我本地的机器上一切都很好 我选择的这个样子

Direction1 title
  Specialization1 title
  Specialization2 title
  ...
Direction2 title
  Specialization3 title
  Specialization4 title
  ...
...

当我将它部署到服务器上时,有一个小小的惊喜。我选择的输出变成这样:

#<Direction:0xb02fb60>
  #<Specialization:0xaa5fb10>
  ...
#<Direction:0x991cf90>
  #<Specialization:0xb02f868>
  ...

它似乎将 :to_s 作为标签方法(和价值方法),而不是:标题

确定问题的方式是明确规定这些方法:

= simple_form_for :some_name do |f|
  = f.input :specialization_id, collection: Direction.all,
            as: :grouped_select,
            group_method: :specializations,
            group_label_method: :title,
            label_method: :title,
            value_method: :id

但我想知道为什么会发生这种事?我不喜欢这种惊喜。

一些细节:

  • 本地机器正在运行 MacOS狮子

  • 发送服务器在Debian Squeeze 上

  • Ruby 版本相同(1.9.3p194 rvm)

  • 铁路版本:3.2.3

  • 与Capistrano一起部署

我还试图在本地用虚拟Debian机器复制它,用于调试目的。但我没有成功。

谁能告诉我发生了什么?

问题回答

我只在生产模式上有同样的行为

= f.input(:city_id, label:  Ciudad , :collection => Country.order(:name), :as =>     :grouped_select, :group_method => :cities, :group_label_method => :name, :label_method => :name, promt: "Por favor seleccione una ciudad", :include_blank => true, :input_html => { class:  span4 } )

我期望得到城市的ID 而不是返回

"#<City:0x007f943d5a2de0>"

<强> UPDATE

我添加了简单形式的道具 < strong > value_ manethod:: id , 对我很好。

= f.input(:city_id, label:  Ciudad , :collection => Country.order(:name), :as =>     :grouped_select, :group_method => :cities, :group_label_method => :name, :label_method => :name, promt: "Por favor seleccione una ciudad", value_method: :id, :include_blank => true, :input_html => { class:  span4 } )

Try to use with f.association
for example:

 = f.association :direction

您也可以调用标签_ 方法来定义选项中的标签定义





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签