Rails 中巧妙的通用形式部分
我有一个货币小部件,我在整个应用程序中使用它。 IE。用户将货币从 EUR -> 更改为美元或大约 13 种其他货币。我还有一些其他用例,例如日期,但这个是最容易解释的。货币基本上只是通过调用货币控制器来更新会话变量,然后使用一些 JS 重新加载页面,但我只想获取页面的某些元素(即反映货币变化而不是其他元素)...
$("#places_list").html("<%= escape_javascript(render :partial => 'places/list') %>");
或者如果另一个控制器
$("#locations").html("<%= escape_javascript(render :partial => 'locations/places') %>");
但这些元素特定于当前控制器,即呈现控制器特定部分,例如列表...(货币数学本身位于应用程序帮助程序中,因此不会发生新逻辑),并且货币控制器很简单,由不同的控制器部分加载。 (位置、地点等)
除了在每个控制器中专门为此目的执行一个操作之外,我怎样才能让它以一种可以渲染特定于当前控制器的元素的方式运行,这样我就可以通过 js 智能地替换它们重新加载? IE。我可以将当前控制器传递给货币,
<%= hidden_field_tag :info, controller.controller_name %>
我不确定这是否有意义,但我希望它有意义,至少在我自己的大脑中,如果不是在其他人的大脑中。
I have a widget for currencies which I use throughout my application. Ie. the user changes the currency from EUR -> USD, or about 13 other currencies. I have a few other use-cases such as dates but this one is the easiest to explain. Currency basically just updates a session variable by calling the currency controller and then uses some JS to reload the page, but I'd like to only fetch certain elements of the page (ie that reflect the currency change and nothing else)...
$("#places_list").html("<%= escape_javascript(render :partial => 'places/list') %>");
or if another controller
$("#locations").html("<%= escape_javascript(render :partial => 'locations/places') %>");
but these elements are specific to the current controller, ie rendering a controller specific partial such as a list... (the currency math itself is in a an application helper, so no new logic is going on), and the currency controller is simple a partial loaded by different controllers. (Locations, Places, etc)
Other than making an action in every controller specific for this purpose, how can I make it behave in a way that I can render elements specific to the current controller, so I can replace them intelligently over js, instead of reloading? Ie. I can pass in to currency the current controller
<%= hidden_field_tag :info, controller.controller_name %>
I'm not sure if any of that makes sense, but I hope it does, at least in my own brain if not in anyone else's.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我尝试了一个小应用程序,下面是一个适合您的解决方案,
首先,就我而言,我想做的是在每个模型中有一个名为 :info 的属性,以便我可以调用它(< ;%= f.text_field :info %>) 在我的视图中(请注意 f 可以是模型的任何实例),
因为我需要向每个模型添加 :info 属性。你有几种方法可以做到这一点
1 - 创建一个类(它继承自 AR::Base 并继承给定类的所有其他模型)(但它需要一些代码更改)
2 - 快速而丑陋的方法是注入我的: AR::Base 类的 info 属性(我就是这样做的;))
在 config/initializers 中创建一个文件,例如 curreny.rb 并
在使用 AR 的任何视图中添加此代码,将具有 :info 属性
Ex: users/新
新用户
请注意:这不是一个很好的解决方案,但我认为有了这个你可以开始思考
欢呼
sameera
I have tried a little app and below would be a one solution for you,
first of all, in my case what I'm trying to do is to have an attribute called :info in each model , so that I can call it (<%= f.text_field :info %>) in my views (please note f can be any instance of a model)
for that I need to add :info attribute to each model. You have several ways of doing it
1 - create a class (which inherits from AR::Base and inherit all the other models from the given class) (but it requires some code changes)
2 - Quick and ugly way is to inject my :info attribute to AR::Base class (Which I did ;) )
create a file in config/initializers say curreny.rb and add this code
after that from any view which u uses AR, will have the :info attribute
Ex: users/new
New user
Please note: this is not a very good solution, but I think with this you can start thinking
cheers
sameera