5

rils の ruby​​ で jtable を実行するタスクがありました。これが私のコントローラーです

 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

これは私のindex.html.erbファイルです

<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>

「サーバーとの通信中にエラーが発生しました」のようなエラーが発生しました。新しいユーザーを追加するとき。新しい生徒を追加するにはどうすればよいですか?

4

2 に答える 2

3

この新しい学生のコントローラーは 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
于 2012-11-28T11:58:05.423 に答える
1

レコードを保存する場合は:remote => true、フォームに追加する必要があり、コントローラーのアクションには次のようなものがあります。

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

私があなたの質問を正しく読んでいて、最後に作成したユーザーだけを返したい場合は、do whatever上記のコードスニペットの部分に次を追加できます。

{'Result' => 'OK','Records' => @students.last(&:attributes)}
于 2012-11-28T08:54:07.360 に答える