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

时间:2015-01-27 09:36:34

标签: ruby-on-rails methods activeadmin

我有 Rails 4 ActiveAdmin 插件。

在广告控制器中我有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修改的新手,请善待。

任何帮助都会很棒。

提前致谢。

1 个答案:

答案 0 :(得分:2)

首先,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

如果您需要特定链接(在图像中我已经更改了与css按钮的链接)除了查看,编辑,删除如下:

enter image description here

然后使用:

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

对于链接操作路径,在控制器中创建一个def(此处为:send_push_notification),然后从那里重定向到admin。

如果您需要更多信息,请与我们联系。