简体   繁体   中英

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.

#_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?

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.

Normally if something like :

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

on the JS side you should post parameters in this way :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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