是否可以在部分中仅放置一个 Rails 表单元素?
我的应用程序有一个选择框供用户选择“地点”。 正如您所期望的,该选择框是一个表单。 我还在页面上的某处执行了通过 AJAX 创建新场地的操作。 创建新场地后,我想更新场地选择框以反映这一点。
我的解决方案是将选择框放入部分中,并通过控制器中的创建操作渲染部分。
<div id="venue_select" style="clear: both;">
<%= render :partial => 'venue/venue_select_box' %>
</div>
部分看起来像这样:
<%= f.collection_select :venue_id, @user_venues, :id, :name, :prompt => 'Select a venue' %>
其中 f 是表单引用:
<% form_for :shows do |f| %>
问题是 f 在部分中未定义,所以我收到错误。 一种解决方案是包含整个表单,但我觉得这应该没有必要,因为我没有更新整个表单。 关于如何解决这个问题有什么想法吗?
My app has a select box for users to choose a "venue". This select box is, as you would expect, in a form. I also have an action somewhere on the page that creates a new venue via AJAX. After the new venue is created, I would like to updated the venue select box to reflect this.
My solution was to put the select box in a partial and render the partial from the create action in the controller.
<div id="venue_select" style="clear: both;">
<%= render :partial => 'venue/venue_select_box' %>
</div>
The partial looks like this:
<%= f.collection_select :venue_id, @user_venues, :id, :name, :prompt => 'Select a venue' %>
where f is the form reference:
<% form_for :shows do |f| %>
The problem is that f is undefined in the partial, so I get an error. One solution would be to include the entire form, but I feel like that should not be necessary because I am not updating the entire form. Any ideas on how to go about this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以像这样将变量传递给部分:
对于这样的东西,查看 文档。
编辑:要将部分与 AJAX 请求一起使用,您需要在 .rjs 模板中“重新创建”表单。 因此,在您的控制器中,您需要再次找到该对象。
然后在你的venue/venue_select.rjs文件中:
其中
:element
是你想要替换的选择菜单的id。基本上,您只需重新创建 form_for ,然后使用它来更新选择字段。
You can pass a variable to the partial like this:
For stuff like this it is always good to look at the documentation.
EDIT: To use the partial with AJAX request you'll need to "recreate" the form in your .rjs template. So in your controller you'll need to find the object again.
Then in your venue/venue_select.rjs file:
Where
:element
is the id of the select menu you want to replace.Basically you just recreate the form_for and then use that to update the select field.
使用 Javascript 将地点添加到选择框可能会更容易,作为 AJAX 添加上的
onComplete
挂钩。It might be easier to add the venue to the selectbox with Javascript, as an
onComplete
hook on your AJAX add.我同意 Sarah Mei 的观点,如果有任何事情让您的 AJAX 调用返回已添加地点的 JSON 表示,然后创建一个新选项并将其添加到 select 元素。
添加选择选项的一些通用 JS 代码:
I agree with Sarah Mei on this, if anything have your AJAX call return a JSON representation of the venue that was added and then create a new option and add it to the select element.
Some general JS code for adding a select option:
这些都是不错的选择,但我认为我找到了最简单的。 基本上,我只是在 collection_select 中硬编码了名称,所以我不需要“f”变量:
然后我的 VenueController 如下:
如果由于任何原因此方法是不好的做法,请告诉我,我会很高兴地相信你答案。
These are all great options but I think I found the easiest. Basically, I just hard coded the name in the collection_select so I would not need the "f" variable:
Then my VenueController is as follows:
If this method is bad practice for any reason, please let me know and I will happily credit you for the answer.
我认为您的解决方案还不错,但是如果您尝试以另一种形式使用此部分,则生成的字段名称会出现问题。
为了避免这种情况,您可以在 ajax 调用中提交 f.object_name 的内容。 在您的情况下,它将是“shows”,并且因为该方法是:venue_id,所以生成的名称将是“shows[venue_id]”。
I think that your solution is not bad, but If you try to use this partial in another form you have a problem with the name of the field generated.
To avoid this you can submit within the ajax call the content of f.object_name. In your case it will be "shows" and because the method is :venue_id the generated name will be "shows[venue_id]".