有谁知道我为什么得到
undefined method `my_method' for #<MyController:0x1043a7410>
Run Code Online (Sandbox Code Playgroud)
当我从ApplicationController子类中调用my_method("string")时?我的控制器看起来像
class MyController < ApplicationController
def show
@value = my_method(params[:string])
end
end
Run Code Online (Sandbox Code Playgroud)
和我的帮手
module ApplicationHelper
def my_method(string)
return string
end
end
Run Code Online (Sandbox Code Playgroud)
最后,ApplicationController
class ApplicationController < ActionController::Base
after_filter :set_content_type
helper :all
helper_method :current_user_session, :current_user
filter_parameter_logging :password
protect_from_forgery # See ActionController::RequestForgeryProtection for details
Run Code Online (Sandbox Code Playgroud) 我的应用程序应呈现html,以便在用户单击ajax-link时进行回答.
我的控制器:
def create_user
@user = User.new(params)
if @user.save
status = 'success'
link = link_to_profile(@user) #it's my custom helper in Application_Helper.rb
else
status = 'error'
link = nil
end
render :json => {:status => status, :link => link}
end
Run Code Online (Sandbox Code Playgroud)
我的帮手:
def link_to_profile(user)
link = link_to(user.login, {:controller => "users", :action => "profile", :id => user.login}, :class => "profile-link")
return(image_tag("/images/users/profile.png") + " " + link)
end
Run Code Online (Sandbox Code Playgroud)
我试过这样的方法:
ApplicationController.helpers.link_to_profile(@user)
# It raises: NoMethodError (undefined method `url_for' for nil:NilClass)
Run Code Online (Sandbox Code Playgroud)
和:
class Helper
include Singleton …Run Code Online (Sandbox Code Playgroud) 是否可以在?中包含/使用Application Helper方法config/initializers/browser_blocker.rb?
我正在使用浏览器gem来检测和阻止旧的非现代浏览器.
Rails.configuration.middleware.use Browser::Middleware do
include ApplicationHelper
redirect_to :controller => 'error', :action => 'browser-upgrade-required' if browser_is_not_supported
end
Run Code Online (Sandbox Code Playgroud)
我正在使用的助手方法:
# test browser version
def browser_is_not_supported
return true unless browser.modern?
return true if browser.chrome? && browser.version.to_i < ENV['BROWSER_BASE_VERSION_GOOGLE'].to_i
return true if browser.firefox? && browser.version.to_i < ENV['BROWSER_BASE_VERSION_FIREFOX'].to_i
return true if browser.safari? && browser.version.to_i < ENV['BROWSER_BASE_VERSION_SAFARI'].to_i
return true if browser.opera? && browser.version.to_i < ENV['BROWSER_BASE_VERSION_OPERA'].to_i
return true if browser.ie? && browser.version.to_i < ENV['BROWSER_BASE_VERSION_MSFT'].to_i
end
Run Code Online (Sandbox Code Playgroud)