2

嗨,大家好 ,

我在我的项目中实现了 rails admin 现在有几件事我目前坚持

  1. 我想要一个链接(标记为发布者)在rails admin中我的用户控制器的列表视图中作为ajax链接使用remote => true在rails中完成的事情,然后为它编写相关的jscode和html代码

  2. 对于上述自定义操作“ mark_as_publisher ”,我定义了这样的配置设置

    config/rails_admin.rb里面

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

    现在自定义动作的定义看起来像这样

    require "rails_admin_mark_as_publisher/engine"
    
    module RailsAdminMarkAsPublisher
    end
    
    require 'rails_admin/config/actions'
    
    module RailsAdmin
      module Config
        module Actions
          class MarkAsPublihser < Base
            RailsAdmin::Config::Actions.register(self)
    
            register_instance_option :collection do
              true
            end
    
            register_instance_option :http_methods do
              [:get,:post]
            end
    
            register_instance_option :route_fragment do
              'mark_as_publisher'
            end 
    
            register_instance_option :controller do
              Proc.new do
                binding.pry
                if request.get?
                  respond_to do |format|
                    format.html { render @action.template_name} 
                  end
                elsif request.post?
                  redirect_path = nil
                  if @object.update_attributes(:manager => true)
                    flash[:success] = t("admin.flash.successful", :name => @model_config.label, :action => t("admin.actions.mark_as_publisher.done"))
                    redirect_path = index_path
                  else
                    flash[:error] = t("admin.flash.error", :name => @model_config.label, :action => t("admin.actions.mark_as_publisher.done"))
                    redirect_path = back_or_index
                  end
                end  
              end
            end  
          end
        end
      end
    end
    

现在app/view/rails_admin/main/mark_as_publisher.erb中相同定义的视图 看起来像这样

<%=  rails_admin_form_for @object, :url => mark_as_publisher_path(:model_name => @abstract_model.to_param, :id => @object.id), :as => @abstract_model.param_key,:method => :post ,:html => { :class => "form-horizontal denser", :data => { :title => "Mark" } } do |form| %>
  <%= form.submit "save" %>
<%end%>

mark_as_publisher 的 get 和 post url 确实属于上面的控制器定义,保存上面的表单会导致错误调用

could not find routes for '/user/5/mark_as_publisher' :method => "post"

有没有人知道我错过了什么

4

1 回答 1

2

抱歉延迟回复,但我也遇到了完全相同的问题。

编辑:我注意到你已经有了这个,你试过重新启动你的服务器吗?

如果您添加以下内容,它将修复它。

  register_instance_option :http_methods do
      [:get,:post]
  end

问题是默认情况下Actions只响应 :get 请求。

如果你跑

 rake routes 

你会看到一些类似的东西

  mark_as_publisher_path     GET     /:model_name/:id/mark_as_publisher(.:format)    rails_admin/main#mark_as_publisher

https://github.com/sferik/rails_admin/blob/master/lib/rails_admin/config/actions/base.rb#L89

于 2013-08-23T12:04:11.667 回答