2013-12-18 46 views
3

我安裝了導軌寶石X編輯:爲什麼x-editable-rails gem拋出一個錯誤:未定義的方法`xeditable?'?

# 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 #<#<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?(方法未定義)

+0

您是怎麼稱呼該方法的? –

+0

將你的'可編輯嗎?'方法放到ApplicationHelper中,然後再試一次 –

+0

@majioa,我不稱之爲方法,它是auth機制的一部分(如x-editable-rails所描述) – static

回答

5

添加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 
+0

謝謝,它解決了'xeditable?'的問題,但同樣的問題出現在'can?'中(正如我在源代碼中看到的,它們都用在if語句中) – static

+0

如果需要額外,只需寫下:'he​​lper_method:xeditable ?,:can?'你實際上不需要'ApplicationHelper'。看我的編輯。 – Victor

+0

'helper_method:xeditable ?,:can?'不起作用。 'can can'' method is undefined – static

相關問題