简体   繁体   English

使用从URL输入表单中收集的JSON对象填充Rails 4表单

[英]Populate Rails 4 form with JSON object gathered from URL input in form

What I am trying to do is fill in a form in Rails 4 with data being received from a JSON object. 我想做的是用从JSON对象接收的数据填写Rails 4中的表单。

#_form.html.erb  
<%= simple_form_for(@tournament) do |f| %>
 <%= f.input :url %>
 <%= f.input :name %>
 <%= f.input :location %>
 <%= f.input :start_time, :as => :hidden %>
 <%= f.input :end_time, :as => :hidden %>
 <%= f.input :entrants, :as => :hidden %>
 <%= f.input :t_id, :as => :hidden %>
 <%= f.input :type, :as => :hidden %>
 <%= f.button :submit %>
<% end %>

How do I take the input URL and use the received JSON object to populate the rest of the fields? 如何获取输入URL并使用接收到的JSON对象填充其余字段?

If I've well understood you're problem is not in the view but in the controller. 如果我很好地理解了您的问题,则不是在视图中,而是在控制器中。 Just check how you're building the @tournament object. 只需检查您如何构建@tournament对象即可。

Normally if something like : 通常,如果类似:

@tournament = Tournament.new(params[:tournament])

on the JS side you should post parameters in this way : 在JS端,您应该以这种方式发布参数:

$.ajax({
    type: "POST",
    url: '/tournaments',
    dataType : 'json',
    data: { tournament: {t_id: 1, ...} }
});

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

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