[英]ruby on rails Controller class instantiate a ruby class?
I am new to RoR .I want my controller to instatiate an existing class from lib . 我是RoR的新手。我希望我的控制器将lib中的现有类实例化。
Collect data in the form of a string and throw the result on the view.erb.Can I do that. 以字符串形式收集数据并将结果扔到view.erb上。
Do i have to create a new model object and that should that model object inturn call the lib class. 我是否必须创建一个新的模型对象,然后该模型对象又调用lib类。
Not really sure what you want to do. 不太确定要做什么。
If you used a library class - a module for example - its automatically instantiated, when you use ' include
' 如果您使用的是库类(例如模块) ,则在使用“ include
”时会自动实例化
If you just have a generic class , and you included it somewhere, then you already have the class object loaded and can call methods on it. 如果您只有一个泛型类 ,并且将它包含在某个地方,那么您已经加载了类对象,并且可以在其上调用方法。 Or you just create an instance manually with ' object = new MyClass
'. 或者您只是使用' object = new MyClass
'手动创建一个实例。 And then call whatever you like on ' object
'. 然后在“ object
”上调用任何您喜欢的object
。
Whatever Information you collect inside the controller method, you can access in the view, when you place an '@'-Symbol before your variable. 无论在控制器方法中收集什么信息,只要在变量前放置“ @”-符号,就可以在视图中访问。 So if you want your show.html.erb look like this: 因此,如果您希望show.html.erb看起来像这样:
<h1>My String:</h1>
<%= @mystring %>
then you have to do something like this in your controller: 那么您必须在控制器中执行以下操作:
def show
...
@mystring = MyClass.get_my_cool_string
...
end
Hope that helps... 希望有帮助...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.