Rails - using partial form part for AJAX and non-AJAX requests

I have a partial form that I use in two places to add new ingredients (in the recipe app). When I first created the application, I wanted the user to be able to add a new ingredient when they filled out a new recipe card. I used a modal popup and AJAX, so that the form of the new ingredient appears, ends, and when the submit button is pressed, some jQuery hide the modal block and update some parameter tags on the page.

From the recipe form, I call the partial component using <%= link_to 'New ingredient', new_ingredient_path, :remote => true %>. My new.html.erb file of my component contains only a partial call:

<%= render 'form' %>

And _form.html.erb looks like this: (other fields are shortened for brevity)

<%= form_for @ingredient, :remote => true do |f| %>
    <%= render 'shared/ingredient_error_messages' %>
    <div class="field">
        <%= f.label :name %>
        <%= f.text_field :name %>
    </div>
    <div class="field">
        <%= f.label :amount %>
        <%= f.text_field :amount%>
    </div>
    <div class="actions">
        <%= f.submit "Save ingredient" %>
    </div>
<% end %>

, , . jQuery , , HTML- . , "" AJAX , .

: . ? , AJAX HTML . , AJAX, , DRY...

+5
1

:

<%= form_for @ingredient, :remote => request.xhr? do |f| %>
+4

All Articles