:name:Symbol Rails 4.1.x collection_select 的未定义方法`merge' [英] undefined method `merge' for :name:Symbol Rails 4.1.x collection_select

查看:30
本文介绍了:name:Symbol Rails 4.1.x collection_select 的未定义方法`merge'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单元素:

<%= f.collection_select :race, :id, Race.all, :id, :name, prompt: true %>

这允许您在我正在创建的文本冒险中选择您的角色种族.目标是下拉所有可用的种族,按名称选择并让参数传递该种族的 id.

This allows you to select your characters race in a text adventure I am creating. The goal is to have a drop down with all available races, select by name and have the params pass the id of that race back.

但是当我加载页面时,我得到 undefined method 'merge' for :name:Symbol.

But when I load the page I get undefined method 'merge' for :name:Symbol.

我查阅了文档,我认为我做得对,但我猜不是?我做错了什么?

I looked up the docs and I think I am doing it right, but I guess not? what am i doing wrong?

推荐答案

f. 表示您在 form_for 块中?这意味着 f.collection_select 的方法签名不同于普通的 collection_select.第一个参数由 FormBuilder 自动提供,所以如果 :race 是表单对象的一个​​属性,我假设它是一个 Character,你只需要:>

The f. indicates you are in a form_for block? Which means the method signature of f.collection_select is different to just plain collection_select. The first parameter is automatically supplied by the FormBuilder, so if the :race is an attribute of the form object, which I assume is a Character, you just need:

<%= f.collection_select :race, Race.all, :id, :name, prompt: true %>

请参阅有关 FormBuilder#collection_select 方法的文档.

这篇关于:name:Symbol Rails 4.1.x collection_select 的未定义方法`merge'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆