I am attempting to render a form via ajax. It works fine for creating a new venue, but when attempting to edit an existing venue the venue data should load into the form. The form (_modal) is rendered when the edit venue link is called, however the form is processed as a new form, with all fields blank. Calling @venue at the binding.pry, we see the correct venue object.
How would I get the form to process as edit rather than new? Ideas:
respond_to do |format|
format.js { *do stuff* }
Add more parameters to form?
Appreciate any suggestions!
_modal.html.erb:
<section id="venue-modal" class='modal' title="Venue Form" class="">
<%= simple_form_for(@venue, remote: true) do |v| %>
<div id='closeButton'><a href='#'>x</a></div>
<% binding.pry %>
<%= v.input :name, label: "Venue Name" %>
<%= v.input :city %>
<%= v.input :state, collection: States.us_states %>
<div class="actions">
<%= v.submit 'Submit', class: 'button' %>
</div>
<% end %>
</section>
Renders following html:
<form accept-charset="UTF-8" action="/venues" class="simple_form new_venue" data-remote="true" id="new_venue" method="post">
Link calling the modal:
<%= link_to edit_venue_path(venue), :remote => true, :class => "edit_venue_link" do %>
Controller:
def edit
id = params[:id]
if id == 'new'
@venue = Venue.new
else
@venue = Venue.find(id)
end
end
Venue.js:
$('.edit_venue_link').on("ajax:success", function(e, data, status, xhr) {
// stuff happens
Replace the modal content with this line of code in modal.js.erb:
$('#venue-modal').html("<%= escape_javascript(render partial: 'venues/modal') %>");
Controller:
def edit
id = params[:id]
if id == 'new'
@venue = Venue.new
else
@venue = Venue.find(id)
end
respond_to do |format|
format.js { render 'venues/modal' }
end
end
Section tags moved out of the modal partial. The form then recognizes @venue and the fields load appropriately.