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?
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
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)}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With