0

アプリケーションのオブジェクトに DELETE アクションを実装するのに問題があります。

Group Controller での削除アクションの一般的なコードを表示するには、次のようにします。

def destroy
    @group = Group.find(params[:id])
    respond_to do |format|
        if @group.delete
            format.html { redirect_to group_path }
            format.json { head :no_content }
        else
            format.html { redirect_to group_path }
            format.json { head :no_content }
        end
    end
end

そして私のビューレイアウトのコード:

<h1>My Groups</h1>

  <table>
    <tr>
      <th>Name</th>
      <th>Users</th>
      <th></th>
      <th></th>
    </tr>

  <% @groups.each do |group| %>
    <tr>
      <td><%= group.name %></td>
      <td>
        <% group.memberships.each do |membership| %>
          <%= User.find(membership.user_id).name %>
          <br/>
        <% end %>
      </td>
      <td><%= link_to 'Show', group %></td>
      <td><%= link_to 'Destroy', group, :confirm => 'Are you sure?', :method => :delete %></td>
    </tr>
  <% end %>
  </table>

  <br />

  <%= link_to 'New Group', new_group_path %>

Rails サーバーのログで 200 OK の応答が返されますが、グループ オブジェクトを削除できません。返されるページは空白の画面です。

Started DELETE "/groups/2" for 127.0.0.1 at 2013-03-19 01:22:01 +0800
Processing by GroupsController#destroy as HTML
  Parameters: {"authenticity_token"=>"aH+z0DlL7NeoVlxda8Td76WdnrH7/G8UyWDqbJcTu9w=", "id"=>"2"}
Completed 200 OK in 0ms (ActiveRecord: 0.0ms)

ActiveRecord は使用されておらず、データベースに変更はありませんでした。過去にはそのような問題はありませんでしたが、最近、どのようなオブジェクトも削除できないことに気付きました。

私はネット上で見つかった同様の問題の解決策を見つけようとしていますが、残念ながらまだ誰もこの問題を抱えていないようです.

更新 1:

以下は私のアプリケーションのレイアウトです:

<!DOCTYPE html>
<html>
    <head>
        <%= csrf_meta_tag %>
      <title>Home</title>

        <%= javascript_include_tag :application %>
        <%= stylesheet_link_tag :application, :media => "all" %>

        <%= javascript_include_tag "/javascripts/main.js" %>
        <%= stylesheet_link_tag "/css/main.css" %>
    </head>
    <body>

        <%= yield %>

    </body>
</html>

しかし、コンソールを使用してオブジェクトを削除すると。できます:

1.9.3-p194 :003 > Group.find(23).delete
  Group Load (0.5ms)  SELECT "groups".* FROM "groups" WHERE "groups"."id" = $1 LIMIT 1  [["id", 23]]
  SQL (3.0ms)  DELETE FROM "groups" WHERE "groups"."id" = 23
 => #<Group id: 23, name: "groupie", created_at: "2013-03-18 15:29:42", updated_at: "2013-03-18 15:29:42", user_id: nil> 
4

2 に答える 2

1

私はちょうど質問に対する解決策を見つけました。私は実際に、すべてのコントローラーの破棄セッションを台無しにする around_filter のコードを書きました。

  around_filter :clear_registration_id_on_destroy_session, only: :destroy


  def clear_registration_id_on_destroy_session
      is_filter_active = (controller_path == 'devise/sessions' && params[:action] == 'destroy')

      if is_filter_active && user_signed_in?
        if current_user.update_attribute(:device_platform, nil)
          logger.info "Updated device platform attributes to nil"
        end

        if current_user.update_attribute(:registration_id, nil)
          logger.info "Updated registration id attributes to nil"
        end

        yield 
      end
  end

問題は、devise:sessions#destroy 以外のコントローラーに対して何も得られなかったことです。

したがって、私のように物忘れが激しい人は、is_filter_active が true でない場合の他の条件について考えることを忘れないでください。

  def clear_registration_id_on_destroy_session
      is_filter_active = (controller_path == 'devise/sessions' && params[:action] == 'destroy')

      if is_filter_active && user_signed_in?
        if current_user.update_attribute(:device_platform, nil)
          logger.info "Updated device platform attributes to nil"
        end

        if current_user.update_attribute(:registration_id, nil)
          logger.info "Updated registration id attributes to nil"
        end

        yield 
      else
        yield
      end
  end
于 2013-03-19T09:48:36.663 に答える
0

オプションが選択されていないため、削除リンクがshowアクションへのリンクとして機能しているようです。method

削除リンクではJSが正しいHTTPメソッドを使用する必要があるため、アプリケーションのレイアウトに適切なJavaScriptを含める必要があります。

Rails> = 3.1を使用している場合は、ページのレンダリングに使用しているレイアウトファイルのヘッダーに次のものがあることを確認してください。

<%= javascript_include_tag :application %>

また、レイアウトにcsrf_meta_tagが含まれていることを確認してください。

<%= csrf_meta_tag %>
于 2013-03-18T17:55:33.440 に答える