我有一个表单元素:
<%= f.collection_select :race, :id, Race.all, :id, :name, prompt: true %>
这允许您在我正在创建的文本冒险中选择您的角色竞赛.目标是让所有可用的比赛下拉,按名称选择并让params传回该比赛的id.
但是当我加载页面时,我得到了undefined method 'merge' for :name:Symbol
.
我查阅了文档,我认为我做得对,但我猜不是吗?我究竟做错了什么?
我正在尝试构建一个灵活的轮播控件,允许内部元素强制更改幻灯片,以及轮播控制自身更改幻灯片
我页面中的示例结构如下所示
<my-carousel>
<div class="slide">
<button @click="$emit('next')">Next</button>
</div>
<div class="slide">
<button @click="$emit('close')">Close</button>
</div>
</my-carousel>
Run Code Online (Sandbox Code Playgroud)
我的旋转木马的模板就像
<div class="carousel">
<div class="slides" ref="slides">
<slot></slot>
</div>
<footer>
<!-- other carousel controls like arrows, indicators etc go here -->
</footer>
</div>
Run Code Online (Sandbox Code Playgroud)
和脚本一样
...
created() {
this.$on('next', this.next)
}
...
Run Code Online (Sandbox Code Playgroud)
访问幻灯片等没有问题,但使用$ emit将无法工作,我似乎无法找到解决此问题的简单方法.
我希望组件可以轻松重用,而不必使用
通常,我的代码使用with
宏来确保在继续操作之前所有必需的数据都可用,但是我想拥有更多细粒度的错误来准确确定其失败原因。
使用文档中的示例:
with {:ok, width} <- Map.fetch(opts, :width),
{:ok, height} <- Map.fetch(opts, :height) do
{:ok, width * height}
else
:error ->
{:error, :wrong_data}
end
Run Code Online (Sandbox Code Playgroud)
我想知道错误元组中是否缺少width
或height
。
我的尝试是使用默认值:
with {:ok, width} <- Map.fetch(opts, :width, {:error, :missing_width}),
{:ok, height} <- Map.fetch(opts, :height, {:error, :missing_height}) do
{:ok, width * height}
else
{:error, reason} = error -> error
end
Run Code Online (Sandbox Code Playgroud)
但这并不特别优雅。有没有更惯用的方式?
我正在使用nested_form添加和删除nested_attributes.
class Text < ActiveRecord::Base
attr_accessible :attachments_attributes
has_many :attachments, :as => :attachable
accepts_nested_attributes_for :attachments
end
Run Code Online (Sandbox Code Playgroud)
和嵌套模型
class Attachment < ActiveRecord::Base
attr_accessible :description, :file
belongs_to :attachable, :polymorphic => true
end
Run Code Online (Sandbox Code Playgroud)
添加附件工作正常,但删除doesent.
单击删除链接后,text[attachments_attributes][0][_destroy]
输入值从此更改为false
,1
所以我认为这不是问题.
我的更新方法:
def update
@text = Text.find(params[:id])
if @text.update_attributes(params[:text])
redirect_to @text, :notice => "Successfully updated text."
else
render :action => 'edit'
end
end
Run Code Online (Sandbox Code Playgroud)
我的更新方法中的params输出是
attachments_attributes:
'0':
description: asdf asdf as fs
_destroy: '1'
id: '2'
'1':
description: ''
_destroy: '1'
id: '3'
'2':
description: …
Run Code Online (Sandbox Code Playgroud)