Andrew Zah
Andrew Zah

Reputation: 103

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?

Upvotes: 0

Views: 542

Answers (1)

Giacomo Macr&#236;
Giacomo Macr&#236;

Reputation: 21

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

Upvotes: 2

Related Questions