简体   繁体   English

尝试以红宝石形式使用下拉菜单

[英]Attempting to utilize a drop down menu in a form in ruby on rails

First off, I'm pretty new to Ruby and Ruby on rails, so bear with me. 首先,我是Ruby和Ruby的新手,所以请多多包涵。

I'm attempting to have my categories section of my form utilize my category table in my db so that when the author wants to select on a category, they can just select the drop down menu and choose one. 我试图让表单的我的类别部分利用我的数据库中的类别表,以便当作者想要选择一个类别时,他们只需选择下拉菜单并选择一个即可。 For now, I've hard coded them and this works 现在,我已经对它们进行了硬编码,并且可以正常工作

<%= form_for(@article) do |f| %>
  <% if @article.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@article.errors.count, "error") %> prohibited this article from being saved:</h2>


    <ul>
      <% @article.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :content %><br>
    <%= f.text_area :content %>
  </div>
  <div class="field">
    <%= f.label :author_id %><br>
    <%= f.text_field :author_id %>
  </div>
  <div class="field">
    <%= f.label :category_id %><br>
    <%= f.select (:title), [['Finance'], ['Marketing'], ['Programming'], ['Leisure'], ['Management'], ['Food']] %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>

<% end %>

but, by switching the categories section to 但是,通过将类别部分切换为

<div class="field">
    <%= f.label :category_id %><br>
    <%= f.select :category_id, Category.all %>
  </div>

it will give me a drop down box containing each hash, and I'm not to sure why, but I can't do Category.all.name to get the names (I'm assuming it is because it's in a hash). 它会给我一个包含每个哈希的下拉框,我不确定为什么,但是我不能用Category.all.name来获取名称(我假设是因为它在哈希中)。 Any help would be great appreciated. 任何帮助将不胜感激。

Have you defined the Category model in your Controller? 您是否在控制器中定义了类别模型? The form can/will only know what the controller knows. 该表格可以/将仅知道控制器知道的内容。 It cannot know any more. 它不再知道了。 If this form is attached to the articles_controller then it will have access to @article, but it won't have access to @category. 如果将此表单附加到articles_controller,则可以访问@article,但不能访问@category。

So this leaves you with a couple options. 因此,这给您提供了两个选择。 Define category somewhere in your controller, OR alternatively (and this would be my suggestion) setup some type of association for your Articles model. 在控制器的某处定义类别,或者(或者,这是我的建议)为您的Articles模型设置某种类型的关联。

Does Article have many Categories? 文章有很多类别吗? This is a great opportunity to setup that has_many association, and then you can access the category model via Article's associations methods. 这是设置has_many关联的绝佳机会,然后您可以通过Article的关联方法访问类别模型。

This is my suspicion, but we really don't have enough information here. 这是我的怀疑,但是我们这里真的没有足够的信息。 Can you post your controller, and your article model so we can see everything that is going on? 您可以发布控制器和文章模型,以便我们看到发生的一切吗? Also, what error do you get specifically? 另外,您会具体遇到什么错误?

It looks like that you need to change you select use options_for_select tag: 看起来您需要使用options_for_select标签进行选择:

<%= f.select :category_id, options_for_select(Category.all.collect {|c| [ c.name, c.id ] }), :include_blank => true %>

if you want to pre-populate category_id 如果您要预填充category_id

<%= f.select :category_id, options_for_select(Category.all.collect {|c| [ c.name, c.id ]}, @article.category_id), :include_blank => true %>

For more information options_for_select 有关更多信息, options_for_select

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM