1

これは以前は機能していましたが、何かを変更したところ、機能しなくなりました。私の人生では、何が変更されたのかわかりません。

基本的に、jquery ドラッグ/ドロップ機能を実装しているページがあり、送信ボタンをクリックすると、ドラッグ/ドロップされたすべての要素の x および y 位置を格納するデータを渡します。

以下の jquery スニペットで、私が使用していることがわかります$.post。次に、コントローラーでデータをデータベースに保存します。

問題は、それparams[:application]が常に nil であることです。アプリケーションデータが含まれていましたが、何かを変更したため、機能しなくなりました。

私の ajax 呼び出しに params[:application] が含まれていないのはなぜですか?

これは私がヒットしているURLです -/applications/1/edit

これは私のjquery送信機能です

$('.organizer').submit(function(e) {
        e.preventDefault();
        e.stopPropagation();
        var dataToSubmit = $(this).serialize();
        var ids = [];
        var field_values = [];
        var x_values = [];
        var y_values = [];
        var success = true;

        console.log('dataToSubmit = '+dataToSubmit);

        $('.organizer .draggable').each(function() {
            // add the draggable value to an array
            field_values.push($(this).find('a').text().trim());
            ids.push($(this).find('a').data('id'));
            x_values.push($(this).closest('td').data('counter'));
            y_values.push($(this).closest('tr').data('counter'));
        });

        for(i = 0; i < field_values.length; ++i) {
            console.log('id = '+ids[i]);
            dataToSubmit += "&field_name="+field_values[i]+"&appfieldid="+ids[i]+"&xposition="+x_values[i]+"&yposition="+y_values[i];
            console.log(decodeURIComponent(dataToSubmit));
            $.post($(this).attr('action'), dataToSubmit).error(function() {
                success = false;
            }); 
        }

        // if all of the ajax calls were successful, show a success message
        if(success) {
            $('h1').after('<div class="nNote nSuccess"><p>Application Updated Successfully!</p></div>');
            $('.nSuccess').delay(5000).fadeOut();
        }

    });

これが私のコントローラーメソッドです

# PUT /applications/1
  # PUT /applications/1.json
  def update
    @application = Application.find(params[:id])
    p params[:appfieldid]
    p params[:xposition]
    p params[:yposition]
    p params[:field_name]
    p params[:application] # WHY IS THIS ALWAYS NIL?
    app_fields = params[:application]
    p app_fields
    app_fields['application_field_attributes']["0"].merge!({"id"=>params[:appfieldid], "xposition"=>params[:xposition], "yposition"=>params[:yposition]})   #app_fields['application_field_attributes']['yposition'] = params[:yposition]
    p app_fields
    respond_to do |format|
      if @application.update_attributes(app_fields)
        #@application.application_details.xposition = params[:xposition]
        #@application.application_details.yposition = params[:yposition]

        @curr_app = ApplicationField.last
    format.html { render :nothing => true }
        format.json { head :no_content }
        format.js # WHY IS THIS NOT CALLED? INSTEAD I'M USING A SUCCESS CALLBACK ON THE $.POST METHOD
      else
        format.html { render action: "edit" }
        format.json { render json: @application.errors, status: :unprocessable_entity }
        format.js { render action: "update" }
      end
    end
  end

生成されたフォーム マークアップは次のとおりです。

<form accept-charset="UTF-8" action="/applications/3" class="organizer" data-remote="true" id="edit_application_3" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="_method" type="hidden" value="put" /><input name="authenticity_token" type="hidden" value="3LOzuiL/PU6HypJ4OeN5H9yrX3Xyk0VT6XpcFYd1wY0=" /></div>
    <fieldset>
        <div class="widget fluid">
            <div class="whead"><h6>New Application</h6><div class="clear"></div></div>
            <table cellpadding="0" cellspacing="0" width="100%" class="tDefault tMedia" id="app_table">
                <thead>
                    <tr>
                        <td width="35%">First Half</td>
                        <td width="35%">Second Half</td>
                    </tr>
                </thead>
                <tfoot>
                    <tr>
                        <td colspan="6">
                            <input class="buttonS bBlue" name="commit" type="submit" value="Submit" />                  
                        </td>
                    </tr>
                </tfoot>
                <tbody>


                    <tr data-counter="0">
                        <td class="droppable nopadding" data-counter="0">
                                    &nbsp;
                                    &nbsp;
                        </td>           
                        <td class="droppable nopadding" data-counter="1">
                                    &nbsp;
                                    &nbsp;
                        </td>
                    </tr>           
                    <tr data-counter="1">
                        <td class="droppable nopadding" data-counter="0">
                                    &nbsp;
                                    &nbsp;
                        </td>           
                        <td class="droppable nopadding" data-counter="1">
                                    &nbsp;
                                    &nbsp;
                        </td>
                    </tr>           
                </tbody>
            </table>
        </div>
    </fieldset>
</form>

編集

ここにいくつかのより多くの情報があります:

# GET /applications/1/edit
  def edit
    @application = Application.find(params[:id])
  end

edit.html.erb

<%= form_for(@application, :html => {:class => "organizer"}, :remote => true) do |f| %>
...
<% @application.application_field.each_with_index do |f, i| %>              
    <tr data-counter="<%= i %>">
        <td class="droppable nopadding" data-counter="0">
        <% @application.application_field.each_with_index do |f2, i2| %>
                    <% if f2.yposition == i && f2.xposition == 0 %>
                        <ul class="subNav noborder">
                            <li class="draggable" style="height: 64px;">
                            <a value="#" data-id="<%= f.id %>" style="height: 47px;">
                                <span class="icos-list2 move"></span>
                                <span class="field_label"><%= f2.field_name %></span>
                                <% if f.field_type == 'textfield' %>
                                    <input type="text" class="textfield" name="textfield" />
                                <% elsif f.field_type == 'dropdown' %>
                                    <!-- TODO - fill in for dropdown -->
                                <% elsif f.field_type == 'checkbox' %>
                                    <!-- TODO - fill in for checkbox -->
                                <% end %> 
                            </a>
                            <%= link_to '<span class=icos-cross></span>'.html_safe, application_field_path(f), :class => 'remove_fields close', :method => :delete, :remote => true %>
                            </li>
                            </ul>   
                        <% end %>
                    <% end %>
                </td>           
            <td class="droppable nopadding" data-counter="1">
                   <% @application.application_field.each_with_index do |f2, i2| %>
                        <% if f2.yposition == i && f2.xposition == 1 %>
                            <ul class="subNav noborder">
                                <li class="draggable" style="height: 64px;">
                                <a value="#" data-id="<%= f.id %>" style="height: 47px;">
                                    <span class="icos-list2 move"></span>
                                    <span class="field_label"><%= f2.field_name %></span>
                                    <% if f.field_type == 'textfield' %>
                                        <input type="text" class="textfield" name="textfield" />
                                    <% elsif f.field_type == 'dropdown' %>
                                        <!-- TODO - fill in for dropdown -->
                                    <% elsif f.field_type == 'checkbox' %>
                                        <!-- TODO - fill in for checkbox -->
                                    <% end %>
                                </a>
                                <%= link_to '<span class=icos-cross></span>'.html_safe, application_field_path(f), :class => 'remove_fields close', :method => :delete, :remote => true %>
                                </li>
                            </ul>
                    <% end %>
                <% end %>
                </td>   
            </tr>           
        <% end %>
<% end %>
4

2 に答える 2

1

dataToSubmit にはアプリケーション パラメータが含まれていますか? それは何ですか?ID?何?

$(this).serialize() の値は何ですか?

dataToSubmit += "&field_name="+field_values[i]+"&appfieldid="+ids[i]+"&xposition="+x_values[i]+"&yposition="+y_values[i];

アプリケーションという名前のフォームに入力フィールドがないと思いますが、フォームコードには表示されません。

また、「スクリプト」形式で呼び出していないため、リクエストは js レスポンスをレンダリングしません。次のようにします。

$.post($(this).attr('action'), dataToSubmit).error(
  function() {
    success = false;
  },'script');

js形式でリクエストを行う必要があります

サーバーログを確認してください。リクエストのすべてのパラメーターとフォーマットが表示されます。コントローラーにすべてを配置する必要はありません。混乱します

于 2013-01-26T18:11:08.867 に答える
0

私が以前に持っていたことを覚えていたことをたくさん試してみることで、

<%= f.fields_for :application_field do |field| %>
...
<% end %>

コントローラーにparams[:application]渡されました。

実際には何もしていないので、なぜこれが機能するのか正確にはわかりませんfield

于 2013-01-27T20:36:07.823 に答える