在轨形成多个输入域输入型号一整数属性 [英] Rails Multiple Input Field in Form to One Integer Attribute in Model

查看:142
本文介绍了在轨形成多个输入域输入型号一整数属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图允许用户输入两个不同的东西在两个不同的下拉菜单,从相同的形式,它将一个整数存储到一个审查表

I am trying to allow a user to input two different things in two different drop down menus from the same form and it will store an integer into a review table.

我希望用户能够在一个下降的另一个降到选择模型名称下跌和制造商下来。结果将在 BAT_ID 整数存入的形式。 (告诉你的用户正在选择哪个BAT)

I want the user to be able to select model_name in one drop down and manufacturer in another drop down. The result will store a bat_id integer into the form. (Telling you which bat the user is selecting)

我看到有关日期和放一对夫妇的问题;时间,但它们直接存储在模型中的值。我试图存储整数 - BAT_ID 这样BAT_ID会的审查模式直接链接到蝙蝠模型

I have seen a couple questions about date & time but they store the values directly in the model. I am trying to store an integer - bat_id so that the bat_id will directly link the review model to the bat model.

例如我发现,接近:

  • How do ruby on rails multi parameter attributes really work (datetime_select)
  • Rails multiple fields to one model attribute
  • Using multiple input fields for one attribute
  • Rails Update Single Attribute with Multiple Fields

我现在的形式为:

<%= form_for(@review) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <div class="field" align= "center">
    <h3>Select Brand</h3>
    <%= f.collection_select :manufacturer_id, Manufacturer.all, :id, :manufacturer, include_blank: true %>
    <h3>Select Bat</h3>
    <%= f.grouped_collection_select :bat_id, Manufacturer.all, :bats, :manufacturer, :id, :model_year_and_name, include_blank: true %>
    <h3>What do you like about this bat?</h3>
    <%= f.text_area :pros, placeholder: "Enter what you like..." %>
    <h3>What do you not like about this bat?</h3>
    <%= f.text_area :cons, placeholder: "Enter what you don't like..." %></br>
  </div>
  <div align="center">
  <%= f.submit "Add Review", class: "btn btn-large btn-info" %>
  </div>
<% end %>

我提交给审核表,并试图这两个提交 BAT_ID 属性。

I am submitting to the review table and trying to submit both of these to the bat_id attribute.

<h3>Select Brand</h3>
<%= f.collection_select :manufacturer_id, Manufacturer.all, :id, :manufacturer, include_blank: true %>
<h3>Select Bat</h3>
<%= f.grouped_collection_select :bat_id, Manufacturer.all, :bats, :manufacturer, :id, :model_year_and_name, include_blank: true %>

在我的蝙蝠模型我有:的has_many:评论&安培;在我的评论我的模型有: belongs_to的:蝙蝠

In my bat model I have: has_many :reviews & In my reviews model I have: belongs_to :bat

更新:是否有可能使用隐藏字段的javascript和我的两个输入的组合,以确定我的一个输出BAT_ID

UPDATE: Is it possible to use a hidden field with the combination of javascript and my two inputs to determine my one output bat_id?

更新我改变了我的下拉code到什么工作,这样我在输入 MANUFACTURER_ID &安培; BAT_ID 都被选中时。不过,我仍然认为这是存储在我的审核模型一个值的方法。我使用JavaScript非常相似,<一个href=\"https://github.com/railscasts/088-dynamic-select-menus-revised/blob/master/store-after/app/assets/javascripts/people.js.coffee\"相对=nofollow>这个

Update I changed my dropdown code to what works so that I enter in manufacturer_id & bat_id when both are selected. However I still think there is a way to store one value in my review model. I am using javascript very similiar to this

推荐答案

由于在其他的答案讨论,你不应该需要存储的 MANUFACTURER_ID 您的审查模式。

As discussed in your other answer, you shouldn't need to store the manufacturer_id on your review model.

我会建议创建一个制造商选择的不是的在你的评价模型访问,但仅仅是用来过滤蝙蝠的名单上表。

I would recommend creating a Manufacturer select that isn't accessed in your Review model, but is simply used to filter the list of bats on the form.

要做到这一点,最好的办法可能是添加一些自定义的数据属性的蝙蝠选择。

The best way to do this is probably to add some custom data attributes to the Bat select.

<%= collection_select :manufacturer, :manufacturer_id, Manufacturer.all, :id, :manufacturer %>
<%= f.select :bat_id, Bat.all.map{ |b| [b.model_year_and_name, b.id, {'data-manufacturer' => b.manufacturer_id}] } %>

然后使用一些JavaScript来过滤蝙蝠选择当制造商选择而改变。

不幸的是,你不能只是设置显示:无来一个选项元素来隐藏它。这并不能掩盖在众多浏览器的选项。所以最好的方法是使用一个比特的jQuery克隆原始选择每一制造商选择的时间被改变,并删除不与所选择的制造商相关联的任何选项。像这样:

Unfortunately you cannot just set display: none to an option element to hide it. This does not hide the option in many browsers. So the best method is to use a bit of jQuery to clone the original select every time the manufacturer select is changed, and remove any option that isn't associated with the selected manufacturer. Like so:

// rename the original select and hide it
$('#bat_id').attr('id', 'bat_id_original').hide();

$('#manufacturer_id').on('change', function() {
    $('#bat_id').remove(); // remove any bat_id selects
    $bat = $('#bat_id_original')
        .clone() // clone the original
        .attr('id', 'bat_id') // change the ID to the proper id
        .insertAfter('#bat_id_original') // place it
        .show()  // show it
        .find(':not(option[data-manufacturer="' + $(this).val() + '"])')
            .remove(); // find all options by other manufacturers and remove them
});

您可能需要更改一些东西得到这个在您的安装工作,但你可以在这里查看的jsfiddle一个静态演示:的 http://jsfiddle.net/JL6M5/

You might need to change a few things to get this to work in your installation, but you can view a static demo on jsFiddle here: http://jsfiddle.net/JL6M5/

您可能需要拒绝的形式 MANUFACTURER_ID 字段提交,avitevet已经指出这个答案应该帮助有:<一href=\"http://stackoverflow.com/questions/4128213/rails-ignoring-non-existant-attributes-passed-to-create\">Rails:忽略通过创建不存在的属性()

You will probably need to reject the manufacturer_id field on form submit, avitevet already pointed out this answer which should help there: Rails: Ignoring non-existant attributes passed to create()

这篇关于在轨形成多个输入域输入型号一整数属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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