从模型内部访问ActionView帮助器 [英] access ActionView helpers from inside the Model

查看:100
本文介绍了从模型内部访问ActionView帮助器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的模板系统,在渲染模板时,该系统调用在模型的不同类(kinda小部件)内部定义的show_me方法.这些小部件最初将html作为字符串返回.所以在我的erb中,我有这样的想法.

I have a simple template system which calls a show_me method defined inside different classes of my model (kinda Widgets) when rendering a template. These widgets originally returned the html as a string. So in my erb I have somethink like this.

<% @template.widgets.each do |widget| %>
   <%= widget.show_me %>    
<% end %>

随着小部件的视图变得越来越复杂,我开始使用局部视图来渲染它们,从小部件内部调用ActionView::Base渲染方法(请不要抛出:)

As the views of the widgets became more complex I start using partials to render them, calling the ActionView::Base render method from inside my widgets (please don't throw up yet :)

def show_me
      # ... specific calculations and other magic.
    ActionView::Base.new(MyApp::Application.config.view_path).render(:partial => "widgets/weather_widget", :locals => {:data => data, :settings => settings})  
end

所以,它的工作原理就像一个咒语(真的...),但是当我想在小部件特定的部分(例如widgets/_weater_widget.html.erb)中使用助手时,它们将无法工作.例如. javascript_tag加薪

So, this works like a charm (really...) but when I want to use helpers inside the widget specific partial (eg. widgets/_weater_widget.html.erb) they don't work. for example. javascript_tag raises

can't convert nil into String
actionpack (3.0.0) lib/action_view/helpers/asset_tag_helper.rb:790:in `join'
actionpack (3.0.0) lib/action_view/helpers/asset_tag_helper.rb:790:in `rails_asset_id'
actionpack (3.0.0) lib/action_view/helpers/asset_tag_helper.rb:813:in `rewrite_asset_path'
actionpack (3.0.0) lib/action_view/helpers/asset_tag_helper.rb:742:in `compute_public_path'
actionpack (3.0.0) lib/action_view/helpers/asset_tag_helper.rb:256:in `javascript_path'
actionpack (3.0.0) lib/action_view/helpers/asset_tag_helper.rb:822:in `javascript_src_tag'
actionpack (3.0.0) lib/action_view/helpers/asset_tag_helper.rb:362:in `block in javascript_include_tag'
actionpack (3.0.0) lib/action_view/helpers/asset_tag_helper.rb:362:in `collect'
actionpack (3.0.0) lib/action_view/helpers/asset_tag_helper.rb:362:in `javascript_include_tag'

我想我在创建ActionView :: Base时会丢失某些东西.

I think I'm missing something when I create the ActionView::Base.

有什么想法如何解决这个问题?也欢迎对总体设计提出任何建议:)

Any thoughts how to solve this? Also any suggestion on the overall design is welcome too :)

推荐答案

敬请期待MVC不会渲染模型中的视图代码. :(总是有一种更好的方法.

Please for the love of all things MVC don't render view code from a model. :( There is always a better way to do it.

示例:

class Widget < ActiveRecord::Base
  def name
    "weather_widget"
  end

  def settings
    ## something here
  end

  def data
    ## calculations here
  end
end

助手

module WidgetHelper

  def render_widget(widget)
    render(:partial => "widgets/_#{widget.name}", :locals => {:data => widget.data, :settings => widget.settings})
  end

end

查看

<% @template.widgets.each do |widget| %>
  <%= render_widget(widget) %>    
<% end %>

很显然,这可能并不是您所处情况的确切解决方案,而只是作为指南,向您展示可以做的一些事情来保持代码干净. :)

Obviously that may not be the exact solution for your situation, but is just a guide to show you something that can be done to keep your code clean. :)

这篇关于从模型内部访问ActionView帮助器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆