- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我在谷歌上搜索并尝试了过去几天我能想到的一切来解决一个相对简单的(我想)has_and_belongs_to_many
关系问题。
我成功地使用 HABTM 关系提交了一个单一的关系值。这是示例代码:
型号:
class Livre < ActiveRecord::Base
has_and_belongs_to_many : auteurs
end
class Auteur < ActiveRecord::Base
has_and_belongs_to_many :livres
end
Controller :
def new
@livre = Livre.new
@auteurs = Auteur.all
end
def create
@livre = Livre.new(livre_params)
if @livre.save
redirect_to [:admin, @livre]
else
render 'new'
end
end
private
def livre_params
params.require(:livre).permit(:name, :auteur_ids)
end
查看:
<% f.label :auteur %><br>
<% f.collection_select(:auteur_ids, @auteurs, :id, :name) %>
发布的参数:
{"utf8"=>"✓",
"authenticity_token"=>"mAXUm7MRDgJgCH00VPb9bpgC+y/iOfxBjXSazcthWYs=",
"livre"=>{"name"=>"sdfsdfd",
"auteur_ids"=>"3"},
"commit"=>"Create Livre"}
但是当我尝试将“multiple true”添加到 View 的 collection_select
助手时,(现在是 multiple)关系不再保存。示例代码:
(模型和 Controller 都不变)
查看:
<% f.label :auteur %><br>
<% f.collection_select(:auteur_ids, @auteurs, :id, :name, {}, {:multiple => true}) %>
发布的参数:
{"utf8"=>"✓",
"authenticity_token"=>"mAXUm7MRDgJgCH00VPb9bpgC+y/iOfxBjXSazcthWYs=",
"livre"=>{"name"=>"sdfsdf",
"auteur_ids"=>["1",
"5",
"3"]},
"commit"=>"Create Livre"}
如您所见,“auteur_ids”的参数现在是一个数组。这是唯一的区别。
我做错了什么?
澄清一下:这两段代码都能够向 livres
数据库表中添加一条新记录,但只有第一段代码能够向 auteurs_livres
数据库表。第二个只是不向 auteurs_livres
中插入任何内容。
(我在 ruby 1.9.3p194 和 rails 4.0.1 上运行)
谢谢!
回答
对于遇到同样问题的好人,这里是答案:
编辑您的 Controller 并将允许的参数从 :auteur_ids
更改为 {:auteur_ids => []}
params.require(:livre).permit(:name, {:auteur_ids => []})
现在可以了:)
最佳答案
对于遇到同样问题的好人,这里是答案:
编辑您的 Controller 并将允许的参数从 :auteur_ids
更改为 {:auteur_ids => []}
params.require(:livre).permit(:name, {:auteur_ids => []})
现在可以了:)
关于ruby-on-rails - 当我将 "multiple true"添加到 collection_select 时,Rails 4 HABTM 停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20345213/
我无法理解两者之间的区别。有人可以解释一下使用 form_for 时的区别吗? 假设你有这个: 示例 - 你什么时候会使用这个? 与使用这个? Rails Api 最佳答案 当您使用 f.some
我的表单中有两个 collection_select 字段。一种是选择类别,另一种是选择子类别。我希望根据选择的类别填充子类别字段。如果有人选择类别 1,则属于类别 1 的子类别将填充在第二个 col
问题: 我需要根据组织集合的选择来过滤单位集合。 选择单位后,单位下拉菜单应刷新以仅显示属于已知单位的单位。 我检查了以下问题: Rails forms: updating collection_se
我有这个简单的表单,我希望能够使用 CSS 设置下拉菜单的样式。 input_html: { class: "form-dropdown"} 没有改变样式。我如何才能正确地将一个类添加到简单表单 co
我试图在我的表单中包含以下 HTML 代码: Please select English Spanish Please select Engli
我有一个 collection_select 允许客户选择另一个客户。我还有一个按钮,单击该按钮时,应该会触发一个 javascript 函数,该函数会找到所选客户的字符串值,并将其添加到页面上的列表
我正在使用 Rails 4 和 Ruby 2.2 构建一个 CAD 应用程序。 我遇到的问题如下: 我正在尝试使用 collection_select(动态选择框)将用户 employee_ident
我认为这个问题的第一个版本太复杂了,并且证明了解决问题的错误尝试。考虑这个假设的迁移文件中的模型: class CreateProjects
我有一个小问题,它真的很烦我。 我在 Controller 中拥有所有标准脚手架代码,以提供标准 CRUD 功能。 collection_select 表单助手在我看来: 链接表有一个 ca
我有以下集合选择,它在 Rails 应用程序中充当过滤器。 "get", :id => "filter_form") do %> 'All'} %> 这总是会生成 select 元素的名称
我正在使用连接到我的机场模型的 collection_select。 claim belongs_to :departure_airport, :class_name => 'Airport', :
以下生成一个从我的用户模型中提取的有效选择下拉列表: "Select a User"} %> 我还有一列 :lastname。 我正在尝试使用类似 :firstname + ""+ :lastnam
我看过How do I set the HTML options for collection_select in Rails?我确信我遗漏了一些明显的东西,但我无法让它发挥作用。 我的选择目前看起来
我正在努力了解 Rails 中的 collection_selects。我可以从数据库表中填充下拉列表,提交选定的选项,然后显示结果。但是,当用户选择编辑条目时,我不知道如何在下拉列表中显示所选选项。
如果在我找不到之前已经回答了这个问题。 我有以下几点: = f.collection_select :sex_id, @sexes, :id, :name 这在 Controller 中: @sexe
我有一个 collection_select 下拉列表,其中包含如下名称的下拉列表: 但是我有一个指向他们所属的组的人的外键。在下拉列表中,我想像这样显示人名和他们旁边的组: 保罗(高尔夫球手)凯文
在 Rails 应用程序中,我试图在选择标签中格式化一个值。 就像是 小写方法没有任何效果。我应该能够以这种方式使用它吗?如果没有,我该怎么办? 谢谢 最佳答案 您可以向模型中添加一个方法并为此添加
在 Rails 4 中,我有 ... 我希望在加载页面时默认选择“选择您所在的国家/地区”。一种方法是使用 javascript(在 dom 加载后选择它)。有没有更简单的方法,例如向
我如何截断 collection_select 中的值 'No Parent'} ) %> 我想缩短该值,但遇到以下错误: 40), {:include_blank => 'No Parent'}
如何按名称订购? true %> 我在这里查看了一些搜索结果,但没有任何效果。附:这是一个 meta_search f.select 表单。谢谢。 最佳答案 您只需从 Carmodel.all 中
我是一名优秀的程序员,十分优秀!