5

I had got a task to do jtable in ruby on rils.This is my controller

 def list
    @students = Student.all
    @jtable = {'Result' => 'OK','Records' => @students.map(&:attributes)}

    respond_to do |format|
      format.html # index.html.erb
      format.json { render :json => @jtable}
    end
  end
def newstudent
    @student = Student.new
    @jtable = {'Result' => 'OK','Record' => @students.last(&:attributes)}
    respond_to do |format|
      format.html # new.html.erb
      format.json { render :json => @jtable }
    end
  end

This is my index.html.erb file

<html>
    <head>
        <%=stylesheet_link_tag "jtable_blue","http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css",:media => "all" %>
        <%= javascript_include_tag "jquery-1.8.3.min","http://code.jquery.com/ui/1.9.1/jquery-ui.js","jquery.jtable.min"%>
<script type="text/javascript">
    jQuery.noConflict();
    jQuery(document).ready(function () {
        jQuery('#StudentTableContainer').jtable({
            title: 'Table of people',
            actions: {
                listAction: '/students/list',
                createAction: '/students/new',
                updateAction: '/student/Update',
                deleteAction: '/student/Delete'
            },
            fields: {
                id: {
                    key: true,
                    create: false,
                    edit: false,
                    list: false
                },
                name: {
                    title: 'name',
                    width: '40%'
                },
                sex: {
                    title: 'sex',
                    width: '20%'
                },
                branch: {
                    title: 'branch',
                    width: '30%'
                }

            }
        });

       jQuery('#StudentTableContainer').jtable('load');
    });
</script>
</head>
<body>

    <div id="StudentTableContainer">
    </div>
</body>
</html>

I had got an error like "An error occured while communicating to the server." while adding a new user. How can i add new student?

2
  • can you explain more what's the question? Commented Nov 28, 2012 at 5:14
  • i edited my question. Hope you can understand. Commented Nov 28, 2012 at 5:18

2 Answers 2

3

Controller for this new student is def newstudent

    @student = Student.new(params.reject {|key,value| key =="controller" || key =="action"})

    if @student.save 
      @jtable = {'Result' => 'OK','Record' => @student.attributes}
    else
     @jtable = {'Result' => 'ERROR','Message'=>'Empty'}
    end  
    respond_to do |format|
      format.html # new.html.erb
      format.json { render :json => @jtable }
    end

  end
Sign up to request clarification or add additional context in comments.

Comments

1

If you want to save the record then you will have to add :remote => true in your form and your controller have something like this in your action:

if @user.save
  respond_to do |format|
    format.js # do whatever
  end
end

If I'm reading your question correctly and you want to return just the last user you created in then in the do whatever part in my code snippet above you can add:

{'Result' => 'OK','Records' => @students.last(&:attributes)}

3 Comments

Thank you for your help. I used this code but by adding time i got an error, it shows like "An error occured while communicating to the server."
sir, i had got the solution for it.
Excellent, be sure to accept this and some of your previous question answers to make sure you get helped in future by the community.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.