Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create json object in ruby on rails?

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?

like image 363
Nithin Viswanathan Avatar asked Oct 06 '22 20:10

Nithin Viswanathan


2 Answers

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
like image 79
Nithin Viswanathan Avatar answered Oct 08 '22 09:10

Nithin Viswanathan


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)}
like image 29
Simpleton Avatar answered Oct 08 '22 10:10

Simpleton