使用 link_to 从 ActiveAdmin 面板执行控制器操作

Execute controller action from ActiveAdmin panel using link_to

我有 Rails 4ActiveAdmin 插件。

在广告控制器中我有 3 个方法。 Bassicaly 我想添加管理功能来延长广告到期日期。共有三个选项:

 def add_two_weeks(advertisement)
   @advertisement = advertisement
     @advertisement.expiration = @advertisement.expiration + 14.days                    
          @advertisement.save 
 end

 def add_eight_weeks(advertisement)
     @advertisement = advertisement
       @advertisement.expiration = @advertisement.expiration + 56.days                    
          @advertisement.save 
 end

 def add_max_weeks(advertisement)
      @advertisement = advertisement
       @advertisement.expiration = @advertisement.expiration + 168.days                    
          @advertisement.save 
 end

admin/advertisement.rb

 ActiveAdmin.register Advertisement do
  scope :all, default: true


  scope :blacklisted do |advertisements|
    advertisements.where("in_blacklist= ?", true).where("admin_confirmed= ?", true)
  end

  scope :waiting_to_blacklist do |advertisements|
    advertisements.where("in_blacklist= ?", true)
  end

  scope :paid do |advertisements|
    advertisements.where("paid= ?", true)
  end




index :as => :grid do |product|
    div do
      a :href => admin_advertisement_path(advertisement) do
        image_tag(advertisement.photo.url(:thumb))
      end
    end
    a truncate(advertisement.name), :href => admin_advertisement_path(advertisement)
  end


  index do
    column :name
    column "Price", sortable: :price do |advertisement|
       advertisement.price
    end
    default_actions
  end


 form do |f|
   f.inputs 'User data' do
         f.input :name
         f.input :email
         f.input :country
         f.input :description
         f.input :description_ru

   end 

     f.inputs 'Additional data' do
         f.input :expiration
         f.input :highlight
         f.input :recomend
         f.input :vip_highlight
         f.input :vip_recomend
   end


  f.inputs 'Blacklist (Information: Both checkboxes need to be checked to blacklist user! )' do

         f.input :in_blacklist
         f.input :admin_confirmed
   end 

   f.inputs 'Change password' do
      f.input :password
      f.input :password_confirmation
  end
  f.actions
end


 show do
   member_action do
        link_to "add 2 weeks", advertisements_add_two_weeks_path(advertisement)
     end

     panel "Recent Reviews" do

          table_for advertisement.reviews.each do |review|
             column(:user)    {|review| link_to(review.user.name, admin_user_path(review.user)) }
             column(:content)    {|review| review.content}
             column("Created at")   {|review| review.created_at.strftime("%B %d, %Y %H:%M")                      }

          end

      end

  end

  sidebar :advertisement_information, :only => :show do
    attributes_table_for advertisement do

      row("User") { auto_link advertisement.name }
      row :email
      row :in_blacklist
      row :admin_confirmed
      row :created_at
    end
  end

  permit_params :name, :email, :country_id, :in_blacklist, :admin_confirmed, :password, :password_confirmation


end

错误: 未定义的方法`member_action'

我借鉴了here的思路,但无法理解其中的原理。所以我即兴发挥。

我不知道这是做这些事情的方法还是我需要完全不同的方法? ActiveAdmin修改新手,望指教

任何帮助都会很棒。

提前致谢。

首先,member_action表示Do operation with single record from perticular one row,collection_action表示Do operation with all records

你可以这样写member_action:

member_action :send_push_notification do
    user = User.find(params[:id])
    redirect_to :action => :show
end

如果你需要特定的link(在图像中我已经将link更改为带有css的按钮)除了查看,编辑,删除这样:

然后使用这个:

actions defaults: true do |gift|
    link_to 'Send Push Notification', send_push_notification_path
end

对于 link 操作路径,在控制器中创建一个 def(此处:send_push_notification)并从那里重定向到管理员。

如果您需要更多,请告诉我。