Note that there are some explanatory texts on larger screens.

plurals
  1. POAJAX Form Issues with Ruby on Rails
    primarykey
    data
    text
    <p>I'm sure this question has been asked before in a different context but I'm still so stuck with figuring out AJAX Rails and, I guess, Rails in general (kinda makes me wonder if I should just go back to PHP...). Well anyways I have this form that I want to AJAXify.</p> <p>This is the "list" view which is part of the "subject" controller</p> <pre><code>&lt;h1&gt;Listing Subjects&lt;/h1&gt; &lt;ul id="subject_list"&gt; &lt;% @subjects.each do |c| %&gt; &lt;li&gt;&lt;%= link_to c.name, :action =&gt; 'show', :id =&gt; c.id %&gt;&lt;/li&gt; &lt;% end %&gt; &lt;/ul&gt; &lt;p id="add_link"&gt;&lt;%= link_to_function("Add a Subject", "Element.remove('add_link'); Element.show('add_subject')")%&gt;&lt;/p&gt; &lt;div id="add_subject" style="display:none;"&gt; &lt;%= form_tag(:action =&gt; 'create') do%&gt; Name: &lt;%= text_field "subject", "name" %&gt; &lt;%= submit_tag 'Add' %&gt; &lt;% end %&gt; &lt;/div&gt; </code></pre> <p>Code for my "subject" controller</p> <pre><code>class SubjectController &lt; ApplicationController def list @subjects = User.find(:all) end def show @subject = User.find(params[:id]) end def create @subject = User.new(params[:subject]) if @subject.save render :partial =&gt; 'subject', :object =&gt; @subject end end end </code></pre> <p>My "subject" partial </p> <pre><code>&lt;li id="subject_&lt;%= subject.id %&gt;"&gt; &lt;%= link_to subject.name, :action =&gt; 'show', :id =&gt; subject.id %&gt; &lt;/li&gt; </code></pre> <p>And the User is just a simple model I made that contains two columns "name" and "email".</p> <p>How this code currently works is that when you click "Add", the textfield input is revealed. When you type something in the input and submit it, the "_show" partial is rendered in the create link. I was following a Rails 2.0 tutorial but I have 3.0 and I've read through some tutorials and they all mention ":remote => true" and jquery_ujs.js but I have no idea how to apply them to a "form_tag" rather than "form_for" Rails helper. </p> <p>Basically I want to asynchronously add the element to the bottom of the list without a page load. I've really tried to understand absolutely all of the tutorials I could find but I just can't figure it out.</p>
    singulars
    1. This table or related slice is empty.
    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. 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