开发者

ruby on rails Controller class instantiate a ruby class?

开发者 https://www.devze.com 2023-01-10 12:08 出处:网络
I am new to RoR .I want my controller to instatiate an existing class from lib . Collect data in the form of a string and throw the result on the view.erb.Can I do that.

I am new to RoR .I want my controller to instatiate an existing class from lib .

Collect data in the form of a string and throw the result on the view.erb.Can I do that.

Do i have to create a new model objec开发者_开发知识库t and that should that model object inturn call the lib class.


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'

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'. And then call whatever you like on '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:

<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...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号