为什么x-editable-rails gem抛出一个错误:未定义的方法`xeditable?'?

 smile 发布于 2023-02-09 14:02

我安装了gem x-editable for rails:

# x-editable
gem 'x-editable-rails'

我将方法添加xeditable?ActionController:

  # Add a helper method to your controllers to indicate if x-editable should be enabled.
  def xeditable?
    true # Or something like current_user.xeditable?
  end

但仍然出现错误:

ActionView::Template::Error (undefined method `xeditable?' for #<#:0x007f9012ee9e78>):
    14: 
    15:     .panel-body
    16:       /a.doc_title.editable id='doc_title_12345' data-name="doc[title]" data-title="Enter doc title" data-type="text" data-url='/docs/12345' href='#doc_title_12345' = doc.title
    17:       = editable doc, :title
    18: 
  app/views/docs/_single_doc_in_accordion.html.slim:17:in `_app_views_docs__single_doc_in_accordion_html_slim__2506304306156466629_70128411437560'
  app/views/docs/index.html.slim:52:in `_app_views_docs_index_html_slim___3263534966956214695_70128384677640'

我应该在哪里定义方法xeditable?以便它开始工作?

更新:

这是application_controller.rb:

class ApplicationController < ActionController::Base

  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

end

这是application_helper.rb:

module ApplicationHelper
  # Add a helper method to your controllers to indicate if x-editable should be enabled.
  def xeditable?
    true # Or something like current_user.xeditable?
  end
end

现在我得到了新的错误:相同xeditable?,但是can?(方法未定义)

1 个回答
  • 加入helper_method :xeditable?你的ApplicationController.rb:

    class ApplicationController < ActionController::Base
    
      helper_method :xeditable?
    
      # Prevent CSRF attacks by raising an exception.
      # For APIs, you may want to use :null_session instead.
      protect_from_forgery with: :exception
    
      # Add a helper method to your controllers to indicate if x-editable should be enabled.
      def xeditable?
        true # Or something like current_user.xeditable?
      end
    
    end
    

    2023-02-09 14:07 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有