Rails:制作“表演”查看和“编辑”查看比赛

发布于 2024-10-28 23:03:40 字数 765 浏览 3 评论 0原文

使用 Rails 2.3.5、Ruby 1.8.7。

是否有任何插件可以更轻松地使我的“显示”、“编辑”和“新”页面具有相同的外观和感觉?我很乐意能够做以下类型的事情。现在,如果我向模型添加一个字段,我必须将其添加到 edit.html.erb 和 view.html.erb 中。我希望能够相当广泛地指定外观,但我希望“编辑”和“显示”看起来相同(两者之间可能有例外,但不多。)

是否有任何 DRY 工具可以实现此目的?

本质上,我希望能够使我的“edit.html.erb”成为:

<% plugin_form_helper_for @model do |form_helper| %>
  <%= render :partial => 'common_look', :locals => {:helper => form_helper} %>
<% end %>

并且 show.html.erb 成为:

<% plugin_show_helper_for @model do |show_helper| %>
  <%= render :partial => 'common_look', :locals => {:helper => show_helper} %>
<% end %>

然后“form_helper”和“show_helper”将实现相同的接口(具有一定的能力)分支以使两者的布局/外观略有不同。)

Using Rails 2.3.5, Ruby 1.8.7.

Is there any plugin that will make it easier to make my "show" and "edit" and "new" pages have the same look and feel? I'd love to be able to do the following type of thing. Right now, if I add a field to my model, I have to add it to both the edit.html.erb and the view.html.erb. I want to be able to specify the look fairly extensively, but I want "edit" and "show" to look the same (With execeptions between the two, perhaps, but not many.)

Is there any DRY tool for this?

Essentially, I'd like to be able to make my "edit.html.erb" to be:

<% plugin_form_helper_for @model do |form_helper| %>
  <%= render :partial => 'common_look', :locals => {:helper => form_helper} %>
<% end %>

and the show.html.erb be:

<% plugin_show_helper_for @model do |show_helper| %>
  <%= render :partial => 'common_look', :locals => {:helper => show_helper} %>
<% end %>

Then the "form_helper" and "show_helper" would implement the same interfaces (with some ability to branch to make slight differences between the layout/look of the two.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟─花易冷 2024-11-04 23:03:40

有插件!

https://github.com/codez/dry_crud

它非常灵活..只要确保你使用正确的Rails 2.3 版本

There is a plugin!

https://github.com/codez/dry_crud

It's very flexible.. just make sure you use the right version for rails 2.3

丿*梦醉红颜 2024-11-04 23:03:40

我用自定义输入/显示辅助函数做了类似您所描述的事情。举个简单的例子,您可以有一个像这样调用的函数:

<%= my_field_helper(:model, :field_name) %>

在控制器中,您将 @context 变量设置为 :show:编辑。帮助程序代码包含 @context 上的开关,如果它是 :edit,它会输出类似的内容

<li>
    <label>Translation of Field</label>
    <input name="model[field_name]" value="???" />
</li>

(可能在中使用通用 @model 变量)所有控制器,因此可以在所有页面上使用相同的函数 - 或将 @my_model 传递给函数本身),但当 @context:show< /code> (或者默认情况下),它输出如下内容:

<li>
    <span class="label">Translation of Field</label>
    <span class="value">Field Value</span>
</li>

所以现在您可以使用相同的代码来显示所有字段!您可能希望将该代码提取到部分代码中,这样您就可以轻松地将其包装在表单中进行编辑,但不是用于显示,但关键部分已经完成 - 如果您需要添加/移动/删除字段,您只需在一个地方执行此操作。

希望这会有所帮助!

I do something like what you're describing with custom input/display helper functions. For a simplified example, you could have a function you would call like this:

<%= my_field_helper(:model, :field_name) %>

And in your controller, you set a @context variable to either :show or :edit. The helper code contains a switch on @context, and if it's :edit, it outputs something like

<li>
    <label>Translation of Field</label>
    <input name="model[field_name]" value="???" />
</li>

(possibly use a generic @model variable in all your controllers, so the same function can be used on all your pages - or pass @my_model to the function itself) but when @context is :show (or maybe by default), it outputs something like this:

<li>
    <span class="label">Translation of Field</label>
    <span class="value">Field Value</span>
</li>

So now you can use the same exact code to show all your fields! You'll probably want to pull that code out into a partial, so you can easily have it wrapped in a form for edit, but not for show, but the critical bit is accomplished - if you need to add/move/delete fields, there's only one place you have to do it in.

Hope this helps!

心凉怎暖 2024-11-04 23:03:40

将该片段包含在 _form.html.erb 中,然后在公共部分(如 show.html.erb)中调用它

然后在您的控制器中,将显示和编辑指向相同的部分(show.html.erb)或类似的内容。

include that snippet in a _form.html.erb then call it in a common partial like show.html.erb

Then in your controller, point show and edit to the same partial(show.html.erb) or something like that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文