- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的应用程序是 Rails API 后端和通过 Nuxt 前端的 VueJS。
我有一个表单,其中一个输入是选择,我正在使用 vue-multiselect 。选择选项是来自不同表的值,我想在其中显示名称字段,但提交 ID。
我能够正常显示下拉列表中的选项,并且我还在表单中提交其他值,但 ID 不起作用。
Rails 控制台显示 distillery_id
不是允许的参数的错误,尽管我确实在 Controller 中设置了此参数。
Started POST "/api/v1/gins" for ::1 at 2019-02-01 13:25:38 +0000
Processing by Api::V1::GinsController#create as HTML
Parameters: {"gin_name"=>"distillery_id", "description"=>"distillery_id should be submitted", "distillery_id"=>{"id"=>3, "distillery_name"=>"Gordon's", "snippet"=>nil, "description"=>nil, "website"=>nil, "country"=>"United Kingdom", "created_at"=>"2019-01-29T13:46:15.088Z", "updated_at"=>"2019-01-29T13:46:15.088Z", "slug"=>nil}, "abv"=>"0", "snippet"=>"distillery_id now?", "gin"=>{"gin_name"=>"distillery_id", "snippet"=>"distillery_id now?", "description"=>"distillery_id should be submitted", "abv"=>"0", "distillery_id"=>{"id"=>3, "distillery_name"=>"Gordon's", "snippet"=>nil, "description"=>nil, "website"=>nil, "country"=>"United Kingdom", "created_at"=>"2019-01-29T13:46:15.088Z", "updated_at"=>"2019-01-29T13:46:15.088Z", "slug"=>nil}}}
Unpermitted parameter: :distillery_id
gins_controller.rb
...
def gin_params
params.require(:gin).permit(:gin_name, :alcoholic, :snippet, :description, :abv, :distillery_id)
end
...
new.vue
<template>
<section class="container">
<div>
<h1>Gins</h1>
<form @submit.stop.prevent="addGin">
<h2>New Gin</h2>
<p>
<label for="gin_name" class="input-label">Title:</label>
<input id="gin_name" v-model="gin_name" type="gin_name" name="gin_name" class="input">
</p>
<p>
<label for="snippet" class="input-label">Snippet:</label>
<input id="snippet" v-model="snippet" type="text" name="snippet" class="input">
</p>
<p>
<label for="description" class="input-label">Description:</label>
<input id="description" v-model="description" type="textarea" name="description" class="input">
</p>
<p>
<label for="abv" class="input-label">ABV%:</label>
<input id="abv" v-model="abv" type="number" name="abv" class="input">
</p>
<div>
<label for="distillery_id" class="input-label">Distillery:</label>
<multiselect
v-model="distillery_id"
track_by="distillery_id"
:options="options"
:searchable="true"
placeholder="Choose One Distillery"
:custom-label="label"
>
</multiselect>
</div>
<p>
<input type="submit" value="Submit" class="button">
</p>
</form>
</div>
</section>
</template>
<script>
import axios from 'axios'
import Multiselect from 'vue-multiselect'
export default {
components: { Multiselect },
data() {
return {
gin_name: '',
snippet: '',
description: '',
abv: '',
distillery_id: '',
options: []
}
},
mounted() {
this.getDistilleries()
},
methods: {
label(option) {
return `${option.distillery_name}`
},
addGin() {
axios.post('http://localhost:4000/api/v1/gins', {
gin_name: this.gin_name, description: this.description, distillery_id: this.distillery_id, abv: this.abv, snippet: this.snippet
})
.then((response) => {})
console.log()
},
getDistilleries(req) {
axios.get('/api/v1/distilleries')
.then((res) => {
this.options = res.data
})
.catch((error) => {
console.log(error)
})
}
}
}
</script>
<style src="vue-multiselect/dist/vue-multiselect.min.css"></style>
<style>
</style>
根据控制台,我怀疑这是 Rails 问题而不是 vue 问题,但允许的参数对我来说看起来不错。
还有什么建议吗?还有什么可能出问题的吗?
最佳答案
注意,这未经测试。但是,快速查看 VueMultiselect 的文档和 Vue 组件后,您的 distillery_id v-model 似乎被设置为单个酒厂对象。我相信 @UdAY 在评论中透露了这一点。因此您的 POST 数据可以更改为:
addGin() {
axios.post('http://localhost:4000/api/v1/gins', {
gin_name: this.gin_name,
description: this.description,
distillery_id: this.distillery_id.id, //this.distillery_id is being set as an object and you just need the id prop here
abv: this.abv, snippet: this.snippet
})
关于javascript - Vue Multiselect 未提交 ID 值(Rails API),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54481676/
所以我有一个带有 x 值的多选框,我需要能够将其移动到另一个多选框,反之亦然。 Volvo Saab Mercedes Audi 需要通过单击按钮将 boxa 的全部或一个值移
我有多选微调器,但是当我选择一个微调器时,微调器关闭了。我想选择几件事。这是我的代码: protected void onCreate(Bundle savedInstanceState) {
我想使用 Bootstrap 多选插件:Bootstrap Multiselect 但这对我不起作用。 $(document).ready(function(){ $('#doc_1')
我有一个页面,我在其中使用 Handlebars 一个接一个地填充两个表单元素。两种形式都有 Bootstrap 多选。 我要开火 $('.multiselect').multiselect() 在呈
我正在尝试检测 JQuery MultiSelect 上的更改以触发另一个位置的更新。更改内容上的普通 javascript 不起作用,因为选择不会更改。我发现了一个名为“beforeclose”的方
我有两个多选框,在更改第一个选择框时,我想禁用第二个选择框的某些组。 我正在使用多选插件的以下代码 onChange 函数 $('optgroup[label=MyGroup2]').prop
我正在使用 Bootstrap-Multiselect ( http://davidstutz.github.io/bootstrap-multiselect/ ) 我有两个下拉列表,当在一个下拉列表
我正在尝试将参数传递给 Vue.js 中的 @select 事件函数 HTML Products
如何在 jquery mutiselect 上调用 onblur 方法?就像在我的多选中一样,如果用户选择红色和绿色,那么我有以下值,然后基于此我需要从数据库获取数据 blue re
我有一个问题,我只知道 jQuery 的基础知识,我需要使用 JavaScript 获取有关列表中所选选项的信息,我浪费了 2 天寻找一个好的方法,但没有成功,请帮助.. 。MultiSelect 的
我有一个表单,其中有 2 个用于依赖列表的选择框。 我正在根据第一个列表中选择的值加载第二个列表,这是多选的。 a value b value c value 对于添加
我试图在我的 jquery 多选中使用 javascript 动态生成选项值。 function prova(data){ var t = 0; var tmp = new A
我想在我的表单中创建一个多选国家/地区下拉列表,让用户可以选择多个国家/地区,并且还可以在需要时删除他们选择的项目。 我可以创建一个下拉列表,从 mysql 表中提取国家名称,但它只允许用户选择一个国
我正在尝试更改 Jquery multiselet 下拉列表的字体大小和宽度,但我找不到如何更改。我正在使用这个 http://www.erichynds.com/blog/jquery-ui-mul
如果看到jqGrid演示: http://www.trirand.com/blog/jqgrid/jqgrid.html 部分:高级->多选 当我移至下一页时,您会看到选中的复选框未保留,并且 再次返
我正在尝试在我的 Bootstrap 多选对象上设置搜索栏。问题是我不知道如何使用 javascript 选项。 我正在使用本教程:www.htmluse.com/bootstrap-multisel
我正在参与一个电子学习项目,并致力于大规模预科类(class)部分。我使用搜索框查找学生的姓名,并将这些值添加到列表中。当我尝试查找其他学生的姓名并将这些值添加到现有列表中时,就会出现问题。这部分会重
我正在使用 Bootstrap Multiselect ( https://github.com/davidstutz/bootstrap-multiselect )。这是一个很棒的组件,但我遇到了下
我的 webpack 配置指定了许多要构建到 vendors.js 中的节点模块。所有模块均随 npm 安装,并驻留在父文件夹 node_modules 中。 文件夹结构: app/scripts/a
我使用 Kendo UI 中的多选。我想知道当用户从多选中删除项目时是否有任何方法可以触发功能。到目前为止,我知道触发了“更改”事件,但它太笼统了,我找不到有关用户删除内容的任何信息。或者有吗? 最佳
我是一名优秀的程序员,十分优秀!