在模型和輔助5共同方法:
選項1:
You can just define the method inside the Client class, and then call it from the view,
like this:
class Client < ActiveRecord::Base
def self.my_class_method
end
def my_instance_method
end
end
And then in your view:
<%= Client.my_class_method %>
<%= @client.my_instance_method %>
選項2:
module MyModule
def my_method
end
end
Include the module in ApplicationHelper so it is available to all your views:
module ApplicationHelper
include MyModule
end
Then in your view you can call it easily:
<%= my_method %>
選項3:
module MyModule
def my_method(amount)
end
end
In your model:
class Client < ActiveRecord::Base
include MyModule
def self.other_method(amount)
my_method(amount)
end
end
Then in your view you can call it easily:
<%= Client.other_method(amount) %>
方案4: 或者你可以聲明helper方法作爲一類功能,並使用它,像這樣:
module Myhelper
def self.my_village
"bikharniyan kallan,Rajasthan,India"
end
end
Then in your view you can call it easily:
<%= Myhelper.my_village %>
選項5: 使用了許多幫手控制器 幫手=>
module Myhelper
def my_info
"my information is"
end
end
module Myhelper1
def my_village
"bikharniyan kallan,Rajasthan,India"
end
end
ApplicationHelper=>
module ApplicationHelper
include Myhelper
include Myhelper1
end
ApplicationController
class ApplicationController < ActionController::Base
include ApplicationHelper
end
YourController
class YourController < ActionController::Base
def action
my_info
my_village
end
end
無,這是一個非常糟糕的做法。助手應該是**查看**助手,如果你需要模型中的助手的東西,它可能會混入你的視圖。 –
謝謝!你讓我意識到我需要在我的視圖中調用Client#my_method ...我一直在盯着這段代碼太長... – Coderama
你的函數有什麼依賴關係?例如它是否使用'request'或'params'? –