如何从 Rails 中的模型渲染视图? [英] how to render a view from the model in Rails?
问题描述
我必须编辑 Rails 应用程序,但在语言方面我是新手(我是 Ruby 和 Rails 的新手).在查看源文件时,我提出了一些问题:
I have to edit a Rails application, but I am a novice when it comes to language (I am new to Ruby & Rails). When viewing the source files I raised some questions:
class Card < ActiveRecord::Base
class CardRenderer < AbstractController::Base
include AbstractController::Rendering
include AbstractController::Helpers
self.view_paths = Rails.application.config.paths["app/views"]
helper_method :res_url
def print(_card, _template)
@card = _card
render template: "prints/#{_template}.html"
end
protected
def res_url(_res_name)
"#{Rails.root}/app/views/prints/res/#{_res_name}"
end
end
.......
.......
.......
end
我看到一个模型在另一个类中包含一个类你有这个语言功能的名字吗?
I saw a model that contained a classe inside another class Do you have a name this language feature?
如何从模型的实例调用这个类?例如来自控制器(显然这不起作用):
How I can call this class from an instance of the model? for example from the controller (Obviously this does not work):
@card::CardRenderer.new.print(@card, @template)
模型类里面的类有一个渲染视图的函数
The class that is inside the model class has a function that renders a view
我可以从控制器使用这个功能吗?例如来自控制器(显然这也不起作用)
I can make use this function from the controller? for example from the controller (obviously this does not work either)
render Card::CardRenderer.new.print(@card, @template)
理想的是我读一本书!,我会做到的!但在这些日子里我不能(时间问题).
Is that the ideal is that I read a book!, And I'll do it! but in these days I can not (timing problems).
推荐答案
您有此语言功能的名称吗?
嵌套类",我认为
如何从模型的实例调用这个类?
@card.class::CardRenderer
应该可以工作
模型类中的类有一个渲染视图的函数.我可以从控制器使用这个功能吗?
从头到尾阅读全文:http://guides.rubyonrails.org/layouts_and_rendering.html
附言我偷偷怀疑应用程序的当前设计完全违背了模型-视图-控制器的想法
p.s. I have a sneaking suspicion the current design of the app goes completely against the ideas of Model-View-Controller
这篇关于如何从 Rails 中的模型渲染视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!