为什么x-editable-rails宝石会抛出错误:undefined method 'xeditable?'

5

我为Rails安装了x-editable宝石:

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

我在ActionController中添加了方法xeditable?

  # 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 #<#<Class:0x007f9012e30f68>: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?(方法未定义)的错误。

你是怎么调用这个方法的? - Малъ Скрылевъ
将你的 xeditable? 方法放到 ApplicationHelper 中,然后再试一次。 - Roman Kiselenko
@majioa,我不会调用该方法,它是认证机制的一部分(正如 x-editable-rails 所描述的那样)。 - static
@Monk_Code,它不起作用。 - static
你是如何将方法添加到控制器中的?能展示一部分代码吗?你将方法添加到了哪个控制器中?如果你将方法添加到了帮助器中,是否已经将该帮助器包含到其控制器中了? - Малъ Скрылевъ
def xeditable?; true; end 下面添加 helper_method :xeditable? - Roman Kiselenko
1个回答

8
在您的ApplicationController.rb中添加helper_method :xeditable?
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

谢谢,这解决了xeditable?的问题,但是同样的问题出现在can?上(我在源代码中看到它们都被用在if语句中)。 - static
如果您需要额外的帮助,请写入以下内容:helper_method :xeditable?, :can? 实际上您不需要 ApplicationHelper。请查看我的编辑。 - Victor
helper_method :xeditable?, :can?不起作用,can?方法仍未定义。 - static
我刚刚定义了一个虚假的 can? 方法(我还没有使用真正的身份验证):def can?(role, object) true # 或者类似于 current_user.xeditable 的东西? end - static
定义了一个新的 can? 方法后,一切都正常吗? - Victor

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接