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

时间:2021-11-07 19:40:38

What I am trying to do is fill in a form in Rails 4 with data being received from a JSON object.

我想要做的是在Rails 4中填写一个表单,其中包含从JSON对象接收的数据。

#_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对象填充其余字段?

1 个解决方案

#1


2  

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, ...} }
});

#1


2  

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, ...} }
});