Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7601941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:17:12+00:00 2026-05-30T23:17:12+00:00

I have a Rails view (recipes/new) which allows users to enter a new recipe.

  • 0

I have a Rails view (recipes/new) which allows users to enter a new recipe. In that view, the user can select items from another model, ingredients. I wanted a way for the users to add a new ingredient without leaving the recipe form.

I created a link for new ingredients which loads a JQuery modal box. Here is the code in my recipes.js file which drives the modal:

assets/javascripts/recipes.js

this code from @coreyward’s answer – see Jquery modal windows and edit object for details – thanks, corey!

$(function(){
    var $modal = $('#modal'),
        $modal_close = $modal.find('.close'),
        $modal_container = $('#modal-container');

    $('a[data-remote]').live('ajax:beforeSend', function(e, xhr, settings){
        xhr.setRequestHeader('accept', '*/*;q=0.5, text/html, ' + settings.accepts.html);       
    });

    $('a[data-remote]').live('ajax:success', function(xhr, data, status){
        $modal
            .html(data)
            .prepend($modal_close)
            .css('top', $(window).scrollTop() + 40)
            .show();
        $modal_container.show();
    });

    $('.close', '#modal').live('click', function(){
        $modal_container.hide();
        $modal.hide();
        return false;
    });
});

My link in the recipes/new.html.erb:

<%= link_to 'New ingredient', new_ingredient_path, :remote => true %>

This works great, in the sense that it loads the modal form with the new ingredient form in there. I can fill out the form, and if the ingredient is saved, I call another function from my controller to close the modal:

ingredients_controller.rb

def create
  if @ingredient.save
    respond_to do |format|
      format.js { render :js => "close_modal();" }
    end
  end
end

My problem is when the form is not filled out correctly. The user clicks and gets no feedback that there are any errors/missing fields, etc… I would like to reload the ingredient form in the popup box, which will render a shared errors partial:

ingredients/new.html.erb

<%= form_for @ingredient, :remote => true do |i| %>
    <%= render 'shared/ingredient_error_messages' %>

    <div class="field">
        <%= i.label :name %>
        <%= i.text_field :name %>
    </div>
    <div class="field">
        <%= i.label :srm %>
        <%= i.text_field :srm %>
    </div>
    <div class="field">
        <%= i.label :price %>
        <%= i.text_field :price %>
    </div>
    <div class="actions">
        <%= i.submit "Save ingredient" %>
    </div>
<% end %>

shared/_ingredient_error_messages.html.erb

<% if @ingredient.errors.any? %>
    <div id="error_explanation">
        <h2><%= pluralize(@ingredient.errors.count, "error") %>
            prohibited this recipe from being saved:</h2>
        <p>There were problems with the following fields:</p>
        <ul>
            <% @ingredient.errors.full_messages.each do |msg| %>

                <li><%= msg %></li>
            <% end %>
        </ul>
    </div>
<% end %>

What’s the best way to get this to work? I can easily create another JS function and call it from the controller if the save is not successful, but I can’t find a way to pass in anything but straight text/html. Is there a way, in the controller, I can parse and output to a function the contents of my ‘new’ view? Is there a better/cleaner way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T23:17:13+00:00Added an answer on May 30, 2026 at 11:17 pm

    Here is a link to a github Rails JQuery-Modal form that was designed by Ramblex to do this very thing:

    https://github.com/ramblex/modal-form

    Here is the respective SO question that it solved (so you can upvote his answer): Rails and modal jquery dialog form

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Rails view that produces a HTML report based on the user
I have this div in my rails view which renders a collection of users:
In my rails project I have a presenter method that has a helper view
I am beginner in rails. I have view that i try to print inside
I have a nested form in a rails view that is called like this
I have a rails form where user has to select a value from a
I have a rails app, that when a user is logged into his account,
I have a rails app that has some simple functionality, allowing the user to
I have rails application that has two view ports that uses the same data.
I have a rails application that calls the helper method in the view by:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.