开发者

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 .

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜