【发布时间】:2016-09-02 12:23:30
【问题描述】:
例如,我有很多类似的页面。只有一个区别:在每个页面上(他们有不同的控制器)他们有不同的变量,适用于这个 html.erb 文件。
例如视频帖子html.erb
<% for post in @posts_for_video>
here some html
and javascript
and also ruby code injection
<% end %>
视频控制器:
@posts_for_video = Post.where(photo: true)
还有我的照片页面:
<% for post in @post_for_photos >
same html as video
same js as video
and same ruby code as video
<% end %>
照片控制器:
@posts_for_photo = Post.where(video: photo)
所以我的问题是:有没有可能将 html+js+ruby_code 放到,例如application_controller.rb?
或者有没有可能将变量作为参数传递给_some.html.erb?
我想,我正在寻找的是(在 application_controller.rb 中):
def posts_for_all(post_variable)
for post in post_variable
html: post.theme
js: post.animation
ruby: some methods
end
end
【问题讨论】:
-
为什么要将它放在
application_controller.rb中?在我看来,将诸如处理登录检测的方法之类的东西放入其中是明智的,并在其中找到特定用户对应于特定事物的内容。 -
即你应该保持干净,并且只保留其他
controller将使用的功能。 -
@Muntasir Alam,关于 application.rb 只是作为示例。但我要说的是,不要在每个 html.erb 中重复代码(并且这个 html 中的唯一区别将是一个变量)。或者 mb 有可能将变量作为参数传递给 html.erb?
-
让我直说。您想要一种始终在页面上显示某些内容的方法吗?如果是这样,我会发布解决方案
标签: ruby-on-rails ruby ruby-on-rails-3