简体   繁体   English

Rails Ajax将其他表单字段插入表单

[英]Rails ajax insert additional form fields into form

I am trying to add a new select fields to my form through the built in rails ajax methods but not really sure how to proceed. 我试图通过内置的Rails Ajax方法向表单中添加一个新的选择字段,但不确定如何继续。 I've looked at other ajax rails SO but if there is a more relevant one that somebody has found that offers direction, that would be helpful. 我已经看过其他的ajax rails,但是如果有人发现了一个更相关的指南,这会有所帮助。

details.html.erb details.html.erb

<%= form_for @event, url: wizard_path do |f| %>
# lots of forms here

    <div id="add-field">

    </div>
    <%= link_to "Add Event Detail", add_event_detail_path, remote: true %>

    <%= f.submit "Next" %>
<% end %>

events/build_controller added this events / build_controller添加了此

def add_event_detail
    respond_to do |format|
        format.js
    end
end

add_event_detail.erb add_event_detail.erb

$("#add-field").append("<%= f.select(:event_detail, options_for_select([['Option1', 'Option1']) %> <%= f.text_field :event_detail %> ");

I also added this route for now 我现在也添加了这条路线

match "/add_event_detail" => "events/build#add_event_detail", via: 'get'

I have a few issues (besides the bad code that you might see). 我有几个问题(除了可能看到的错误代码之外)。 How do you pass the f variable into the jquery append method and what would be a good way to deal with unique name's for each newly created form essentially, :event_detail1 :event_detail2 etc. Ideally I would want to link that name to the selection in the form 如何将f变量传递到jquery append方法中,以及从本质上来说,这是处理每个新创建的表单的唯一名称的好方法:event_detail1 :event_detail2等。理想情况下,我想将该名称链接到形成

eg :location for the option :location 例如:location为选项:location

have you tried to look at nested_form gem ie( https://github.com/ncri/nested_form_fields ), it can dynamically add fields to a form. 如果您尝试查看nested_form gem ie( https://github.com/ncri/nested_form_fields ),它可以将字段动态添加到表单中。 or try to look at RailsCasts #403 Dynamic Forms it will give ua clear direction. 或尝试查看RailsCasts#403动态表格,它将为您提供明确的方向。 Hope this helps u to the right direction. 希望这可以帮助您朝正确的方向发展。

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

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