Topic: Problem with incoperating ajax into my application

I have created my basic library application. Now I created a new subject controller so that if they want they can add new subjects also.But the problem is inside the index page ,apart from listing all subjects there is a link which on click a form will b displayed to enter the subject name.But the form creates an error "undefined method `model_name' for NilClass:Class".

Some one please help me.....

Re: Problem with incoperating ajax into my application

Can you post some code to help us understand ...

What JS libraries are you using?

Last edited by jmesserer (2011-03-24 13:30:02)

Re: Problem with incoperating ajax into my application

This is the code in my index.html .erb of the subjects view.I am trying to add a form in this page.But its saying "undefined method `model_name' for NilClass:Class" in the form file.
<h1>Listing Subjects</h1>
<ul id="subject_list">
<% @subjects.each do |c| %>
<li><%= link_to c.name, :action => 'show', :id => c.id %>
<%= "(#{c.books.count})" -%></li>
<% end %>
</ul>
<p id="add_link"><%= link_to_function("Add a Subject",
"Element.remove('add_link'); Element.show('add_subject')")%></p>

<div id="add_subject" style="display:none;">
<%= form_for(@subject) do |f|%>

    <%= f.label :name%><br />
    <%= f.text_field :name%>

<% end %>

</div>

Re: Problem with incoperating ajax into my application

Is it safe to assume that you are setting @subject in the controller (@subject = Subject.new)?

Re: Problem with incoperating ajax into my application

Ok thanks a  lot..But still am confused.How to implement ajax with this form.what am trying to do this when i enter the name and press submit it should get listed above with other subjects

Re: Problem with incoperating ajax into my application

jmesserer wrote:

Is it safe to assume that you are setting @subject in the controller (@subject = Subject.new)?


Thanks for that.Now what my problem is am getting the response from the partial when am viewing through firebug.But its showing only when the page refreshes.

I am pasting here the code for each:

SUBJECTS vONTROLLER
class SubjectsController < ApplicationController
    layout 'standard'
    def index
        @subject = Subject.new
        @subjects = Subject.all
       
        respond_to do |format|
          format.html # index.html.erb
          format.xml  { render :xml => @subjects }
        end
    end
    def show
        @subject = Subject.find(params[:id])

    end
    def create
        @subject = Subject.new(params[:subject])
        if @subject.save
             render :partial => "subject", :object => @subject

        end

    end

end

INDEX FILE IN THE SUBJECTS VIEW
<h1>Listing Subjects</h1>
<ul id="subject_list">
<% @subjects.each do |c| %>
<li><%= link_to c.name, :action => 'show', :id => c.id %>
<%= "(#{c.books.count})" -%></li>
<% end %>
</ul>
<p id="add_link"><%= link_to_function("Add a Subject",
"Element.remove('add_link'); Element.show('add_subject')")%></p>

<div id="add_subject" style="display:none;">
<%= form_for (@subject,:remote => true,:update => "subject_list", :position => :bottom,
    :html => {:id => 'subject_form'}) do |f|%>
<div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

</div>

PARTIAL SUBJECT
<li id="subject_<%= subject.id %>">
<%= link_to subject.name, :action => 'show', :id => subject.id %>
<%= "(#{subject.books.count})" -%>
</li>



sOME ONR PLEASE HELP ME