2012-12-02 52 views
0

我使用rails_admin在單擊按鈕時向所有用戶發送電子郵件。該動作被調用兩次而不是一次。rails_admin收集操作執行兩次

的lib/rails_admin_email_everyone.rb:

require 'rails_admin/config/actions' 
require 'rails_admin/config/actions/base' 


module RailsAdmin 
    module Config 
    module Actions 
     class EmailEveryone < RailsAdmin::Config::Actions::Base 
     register_instance_option :visible? do 
      authorized? 
     end 

     register_instance_option :link_icon do 
      'icon-envelope' 
     end 

     register_instance_option :collection? do 
      true 
     end 

     register_instance_option :member? do 
      false 
     end 

     register_instance_option :controller do 
      Proc.new do 
      @topics_with_owners = Topic.where('user_id IS NOT NULL') 

      @topics_with_owners.each do |topic|    
       @reminder = Reminder.new(:topic_id=> topic.id, 
       :to_user_id => student.id, :from_user_id => current_user.id) 
       if @reminder.save 
       TopicMailer.reminder(@reminder.topic).deliver 
       @reminder.update_attribute(:sent, true) 
       else 
       logger.error "Error sending email reminder: " 
        + @reminder.errors.full_messages.map {|msg| msg}.join(',') 
       end 
      end 

      flash[:success] = "Reminders successfully sent." 

      redirect_to back_or_index 
      end 
     end 
     end 
    end 
    end 
end 

配置/初始化/ rails_admin.rb:

require Rails.root.join('lib', 'rails_admin_email_everyone.rb') 
RailsAdmin.config do |config| 
    config.authenticate_with do 
    redirect_to(main_app.root_path, flash: 
    {warning: "You must be signed-in as an administrator to access that page"}) 
    unless signed_in? && current_user.admin? 
    end 

    module RailsAdmin 
    module Config 
     module Actions 
     class EmailEveryone < RailsAdmin::Config::Actions::Base 
      RailsAdmin::Config::Actions.register(self) 
     end 
     end 
    end 
    end 

    config.actions do 
    # root actions 
    dashboard      # mandator y 
    # collection actions 
    index       # mandatory 
    new 
    export 
    history_index 
    bulk_delete 
    # member actions 
    show 
    edit 
    delete 
    history_show 
    show_in_app 

    email_everyone do 
     visible do 
     bindings[:abstract_model].model.to_s == "Reminder" 
     end 
    end 
    end 
end 

當我檢查我的日誌我看到它執行了兩次,但略有不同的參數。一個包括pjax。我發現this有關類似問題的pjax超時計算器問題:

Started GET "/admin/reminder/email_everyone?_pjax=%5Bdata-pjax-container%5D" for 127.0.0.1 at 2012-12-01 20:28:12 -0900 
Processing by RailsAdmin::MainController#email_everyone as HTML 
    Parameters: {"_pjax"=>"[data-pjax-container]", "model_name"=>"reminder"} 
    User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1 
    Topic Load (1.3ms) SELECT "topics".* FROM "topics" WHERE (user_id IS NOT NULL) 
    CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1 
    (0.2ms) BEGIN 
    CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1 
    CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1 
    Topic Load (0.5ms) SELECT "topics".* FROM "topics" WHERE "topics"."id" = 4398 LIMIT 1 
    SQL (6.5ms) INSERT INTO "reminders" ("created_at", "from_user_id", "sent", "topic_id", "to_user_id", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["created_at", Sun, 02 Dec 2012 05:28:12 UTC +00:00], ["from_user_id", 1], ["sent", false], ["topic_id", 4398], ["to_user_id", 1], ["updated_at", Sun, 02 Dec 2012 05:28:12 UTC +00:00]] 
    (1.1ms) COMMIT 
    User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1 
    Rendered topic_mailer/reminder.text.erb (375.8ms) 


Started GET "/admin/reminder/email_everyone" for 127.0.0.1 at 2012-12-01 20:28:14 -0900 
Processing by RailsAdmin::MainController#email_everyone as HTML 
    Parameters: {"model_name"=>"reminder"} 
    User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1 
    Topic Load (1.1ms) SELECT "topics".* FROM "topics" WHERE (user_id IS NOT NULL) 
    CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1 
    (0.2ms) BEGIN 
    CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1 
    CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1 
    Topic Load (0.5ms) SELECT "topics".* FROM "topics" WHERE "topics"."id" = 4398 LIMIT 1 
    SQL (7.1ms) INSERT INTO "reminders" ("created_at", "from_user_id", "sent", "topic_id", "to_user_id", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["created_at", Sun, 02 Dec 2012 05:28:14 UTC +00:00], ["from_user_id", 1], ["sent", false], ["topic_id", 4398], ["to_user_id", 1], ["updated_at", Sun, 02 Dec 2012 05:28:14 UTC +00:00]] 
    (1.0ms) COMMIT 
    User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1 
    Rendered topic_mailer/reminder.text.erb (368.9ms) 

回答

1

我發現了同樣的問題,

我剛添加以下代碼的lib/rails_admin_custom_action.rb

即增加了pjax?選項設置爲false

module RailsAdmin 
    module Config 
    module Actions 
     class EmailEveryone < RailsAdmin::Config::Actions::Base 

     register_instance_option :pjax? do 
      false 
     end 

     end 
    end 
    end 
end 

可能,這將幫助你..