簡體   English   中英

為自定義操作定義控制器似乎不起作用Rails Admin

[英]Define Controller for the custom action doesnot seem to work Rails Admin

嗨,大家好 ,

我在我的項目中實現了Rails管理員現在有幾件事我目前停留在

  1. 我想要一個鏈接(標記為發布者)在rails admin中以我的用戶控制器的列表視圖作為ajax鏈接,這是在rails中使用remote => true完成的 ,之后在其中編寫相關的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中相同定義的View看起來像這樣

<%=  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"

是否有人對我所缺少的有所了解

很抱歉延遲回復,但我也遇到了同樣的問題。

編輯:我注意到您已經有了這個,是否嘗試過重新啟動服務器?

如果添加以下內容,它將解決此問題。

  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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM