Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to separate the things that are server-side (Rails, controllers, actions) and client-side (popups, JavaScript, sending requests).</p> <p>Your client-side actions (JavaScript code) should think about your Rails application as about <em>some</em> server, which returns <em>some</em> responses for <em>some</em> requests. From JavaScript's point of view it is not important whether the server is running Rails or Smalltalk.</p> <p>The basic workflow for your popup may be:</p> <p><strong>1) open a new window</strong> - This requires <em>client-side</em> activity of JavaScript. Use <a href="https://developer.mozilla.org/en/DOM/window.open">window.open</a> function, or (I humbly consider this method a better one) <a href="https://developer.mozilla.org/en/DOM/document.createElement">create a new</a> <code>&lt;div&gt;</code> and style it with CSS so it will cover (a nice effect is an half-opaque background: <code>background: #ddf; opacity: 0.5;</code>, through which you still see the content of the page).</p> <p><strong>2) fill the window with an edit form</strong> - Here your <em>client-side</em> script should make an AJAX-like call <em>(not necessarily real AJAX, since in this case a synchronic request may be sensible)</em> to get the edit form from your Rails application. See this simplicated example:</p> <pre><code>function fillPopupBody(popup) { var req = new XMLHttpRequest(); req.open("GET","/something/partial_new_form.html",false); // Synchronic request! req.send(null); if( req.status == 200 ) { popup.innerHTML = req.responseText; return true; } else { popup.innerHTML = "&lt;p class='error'&gt;"+req.status+" ("+req.statusText+")&lt;/p&gt;"; return false; } } </code></pre> <p><strong>3) Prepare the form-returning action in your Rails application</strong> - <em>(server-side)</em> Usually this may be your <code>new</code> action of the given resource controller, but rendered without layout.</p> <p>An alternative approach would be to build the form by the JavaScript (without fetching it separately), or to include it always in the page - just hidden by default, so the JavaScript needs only to set its <code>display</code> property.</p> <p><strong>4) Handle form submission</strong> - you probably want the user to stay on the same page, so you should intercept the form submission. Just add a handler to the "submit" event of the created form, build a request, post it, check the server response.</p> <p>The <strong>response will be returned by Rails, by your :create action</strong>. You may already have it ready, because the code created by <code>./script/rails generate scaffold</code> is usually OK.</p> <p>If the response is 201 (created), you may close the popup (<code>display: none</code>) and update the page to display the new object - either refresh the whole page, or fetch only the changed parts.</p> <p>If the <code>create</code> action cannot create the object, by default the response code is 422 (unprocessable entity) and the content is the model errors object, rendered as JSON or XML. Just display the errors in the form (let your JavaScript set the <code>innerHTML</code> of some predefined element).</p> <hr> <p>That was the 'manual' way of doing the task. I have some aversion (hardly explainable ;-) to JavaScript libraries, and I prefer to work directly with DOM. You may find a lot of Rails helpers which will save you from writing JavaScript code. I guess that looking at the <code>:remote =&gt; true</code> parameter of <code>form_for</code> will be a good starting point.</p> <p>Also the jQuery documentation ( <a href="http://docs.jquery.com/Main_Page">http://docs.jquery.com/Main_Page</a> ) may be a good thing to read.</p> <p>Ending this long story, here are the <em>short</em> answers to your questions from the comment:</p> <blockquote> <p>How do i link controller actions from the popup?</p> </blockquote> <p>: By sending a HTTP request to the proper URL: "GET /users/1/notes/new", "POST /user/1/notes"</p> <blockquote> <p>How do i close the popup?</p> </blockquote> <p>: By setting <code>display: none</code> if the popup is an element of the page, or by calling <code>window.close()</code> if your popup is a separate window.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload