在Rails 4中使用常量作为选择框的选项(Use a constant as options for select box in Rails 4)

编程入门 行业动态 更新时间:2024-10-26 23:37:21
在Rails 4中使用常量作为选择框的选项(Use a constant as options for select box in Rails 4)

我有一个模型( Bar ),需要一个可能适用于记录的种族属性列表。 该模型是模型Foo的嵌套属性(我认为这不重要,但只是彻底......)。 我没有创建一个模型来管理这个短列表,而是在我的应用程序中定义了一个常量。 然后我可以在验证中引用该常量等。在验证模型时它工作正常 - 我的所有测试都是绿色的。

我还可以在视图中看到值列表(在Bar模型的new.html.haml视图中)。 我可以在这些项目中进行选择,参数看起来是正确的。 但是,所选值不会发布到数据库。

模型Bar有一列:ethnicity类型:字符串。 用于定义选项范围的常量是:

ETHNIC_GROUPS = [nil, "African American/Black", "American Indian", "Hispanic", "Puerto Rican", "Alaskan Native", "Asian American/Pacific Islander", "Mexican American/Chicano"]

在视图文件中,我为模型创建了一个选择框:

#app/views/bar/new.html.haml = form_for([@foo, @bar]) do |f| # form elements omitted = f.label :ethnicity = f.select :ethnicity, CommonConstants::ETHNIC_GROUPS = f.submit "Create New Bar"

在前端,我看到ETHNIC_GROUPS常量中的选项可供选择。 选择元素后,参数显示正确:

--- !ruby/hash:ActionController::Parameters utf8: "✓" authenticity_token: YKvYw3T6q+6DsLA6qdH2yYvFtDAOuBFggCMDclIHlvQ0DvJbZqSdniLMZCivmWINjb3ZoCVWF890Mu4vB3VHrw== bar: !ruby/hash:ActionController::Parameters name: Test Hispanic Selection # parameters omitted for clarity ethnicity: Hispanic # parameters omitted for clarity notes: '' commit: Add New Bar controller: bars action: create foo_id: '4'

据我所知,参数中的所有内容看起来都完全正确。 但是,在成功创建该记录后,种族列中的数据库中没有任何内容。 SQL查询(在postgres中)显示了这个:

select name, ethnicity from bar; name | ethnicity ----------------------------------------------------+----------- African American | American Indian | Hispanic |

只是为了衡量标准,以下是Bar的验证:

#app/models/bar.rb class Bar < ActiveRecord::Base belongs_to :foo validates :ethnicity, inclusion: { in: CommonConstants::ETHNIC_GROUPS }, allow_nil: true end

我真的很难过。 无法弄清楚为什么常量定义的选择数组中的值没有传递给数据库 - 即使它似乎传递给了params散列。

**编辑**

只是想在有用的情况下添加html:

<select name="bar[ethnicity]" id="bar_ethnicity"> <option value=""></option> <option value="African American/Black">African American/Black</option> <option value="American Indian">American Indian</option> <option value="Hispanic">Hispanic</option> <option value="Puerto Rican">Puerto Rican</option> <option value="Alaskan Native">Alaskan Native</option> <option value="Asian American/Pacific Islander">Asian American/Pacific Islander</option> <option value="Mexican American/Chicano">Mexican American/Chicano</option> </select>

I have a model (Bar) that requires a list of ethnic attributes that may apply to the record. The model is a nested attribute of model Foo (I don't think that matters, but just being thorough...). Rather than create a model to manage this short list, I've defined a constant in my application. I can then reference that constant in validations, etc. It works fine when validating the model - all my tests are passing green.

I can also see the list of values in the view (in the new.html.haml view for the Bar model). I can select among those items, and the parameters look correct. However, the value selected is not posting to the database.

The model Bar has a column :ethnicity of type:string. The constant used to define the range of options is:

ETHNIC_GROUPS = [nil, "African American/Black", "American Indian", "Hispanic", "Puerto Rican", "Alaskan Native", "Asian American/Pacific Islander", "Mexican American/Chicano"]

In the view file I've created a selection box for the model:

#app/views/bar/new.html.haml = form_for([@foo, @bar]) do |f| # form elements omitted = f.label :ethnicity = f.select :ethnicity, CommonConstants::ETHNIC_GROUPS = f.submit "Create New Bar"

In the front end, I see the options in the ETHNIC_GROUPS constant available for selection. After selecting an element, the parameters appear correct:

--- !ruby/hash:ActionController::Parameters utf8: "✓" authenticity_token: YKvYw3T6q+6DsLA6qdH2yYvFtDAOuBFggCMDclIHlvQ0DvJbZqSdniLMZCivmWINjb3ZoCVWF890Mu4vB3VHrw== bar: !ruby/hash:ActionController::Parameters name: Test Hispanic Selection # parameters omitted for clarity ethnicity: Hispanic # parameters omitted for clarity notes: '' commit: Add New Bar controller: bars action: create foo_id: '4'

As far as I can see, everything in the parameters looks exactly correct. However, after successfully creating that record, there is nothing in the database in the ethnicity column. A SQL query (in postgres) shows this:

select name, ethnicity from bar; name | ethnicity ----------------------------------------------------+----------- African American | American Indian | Hispanic |

Just for good measure, here are the validations for Bar:

#app/models/bar.rb class Bar < ActiveRecord::Base belongs_to :foo validates :ethnicity, inclusion: { in: CommonConstants::ETHNIC_GROUPS }, allow_nil: true end

I'm really stumped here. Can't figure out why the value in the selection array defined by the constant isn't passing through to the database - even though it appears to be passing into the params hash.

** EDIT **

Just thought to add the html in case that is helpful:

<select name="bar[ethnicity]" id="bar_ethnicity"> <option value=""></option> <option value="African American/Black">African American/Black</option> <option value="American Indian">American Indian</option> <option value="Hispanic">Hispanic</option> <option value="Puerto Rican">Puerto Rican</option> <option value="Alaskan Native">Alaskan Native</option> <option value="Asian American/Pacific Islander">Asian American/Pacific Islander</option> <option value="Mexican American/Chicano">Mexican American/Chicano</option> </select>

最满意答案

确保您已将ethnicity列入白名单:

def bar_params params.require(:bar).permit(:ethnicity) # ... end

Ensure that you have whitelisted the ethnicity param:

def bar_params params.require(:bar).permit(:ethnicity) # ... end

更多推荐

本文发布于:2023-07-18 04:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1154947.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:常量   选项   Rails   constant   box

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!