(Ruby,Rails)CRUD在单个页面上嵌套了超过4个级别的模型...? [英] (Ruby,Rails) CRUD nested models more than 4 levels deep on a single page...?

查看:64
本文介绍了(Ruby,Rails)CRUD在单个页面上嵌套了超过4个级别的模型...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管有许多令人惊奇的信息,但它似乎经常达不到我痴呆的要求.也就是说,我正在寻找一种机制来处理单个页面上的多个嵌套模型.

As much amazing info as is out there, it often seems to fall just short of my demented requirements. That said, I'm looking for a mechanism by which to handle multiple nested models on a single page.

现在,我已经看过所有关于嵌套两个模型(Railscasts等)的视频和帖子(不是,但是很幽默).但是,我需要处理嵌套在4层深处的模型,同时始终使用Javascript来保持页面整洁.

Now, I've seen all the videos and posts (not really, but humor me) on nesting two models (Railscasts, etc.). However, I need to deal with models nested 4 deep, all the while using Javascript to keep the page clean.

基本上,我具有站点->建筑物->控制器->测量,并且想要在单个页面上管理(CRUD)整个站点.我敢肯定它是 possible ,但是我还没有看到一种比较干净的方法可以缠住我的头.如果任何人有任何输入,我将全神贯注(或视情况而定).

Basically I have Site -> Buildings -> Controllers -> Measurements and would like to manage (CRUD) a complete site on a single page. I'm sure it's possible, but I have yet to see a reasonably clean method around which I can wrap my head. If anyone has any input, I'm all ears (or eyes as the case may be).

谢谢.

推荐答案

一些熟练的程序员推荐

Some skilled programmers recommend only nesting resources 1 level deep. Certainly your domain can be more complex, but you shouldn't expose all of that complexity in a single view. If you really need to manage an entire Site on a single page, I recommend you use multiple forms and update the various displays via AJAX, rather than trying to do it all in one form. You'll have better usability and cleaner code.

已编辑

好的,这是HAML中的示例视图:

Okay, here's a sample view in HAML:

%h1 Editing Site
#site-form
  - form_for @site, :class => 'remote', :'data-update' => '#site-form' do |f|
    %p
      = f.label :name
      = f.text_field :name
    %p
      [All the other fields on your Site model]
    %p
      = f.submit "Save Site"


%h2 
  Buildings for 
  = @site.name
#buildings-forms
  - for building in @site.buildings
    %div{ :id => "building-#{building.id}" }
      - form_for building, :class => 'remote', :'data-update' => "#building-#{building.id}" do |f|
        %p
          = f.label :name
          = f.text_field :name
        %p
          [All other building fields]
        %p
          = f.submit "Save Building"    
      %h3 
        Controllers for
        = building.name
        - for cntroller in building.controllers
          %div{ :id => "controller-#{cntroller.id}"}
            - form_for cntroller, :class => 'remote', :'data-update' => "#controller-#{cntroller.id}" do |f|
              %p
                = f.label :name
                = f.text_field :name
              %p
                [All other controller fields]
              %p
                = f.submit "Save Controller"

下一个级别Measurements看起来几乎一样.

And the next level, Measurements, will look pretty much the same.

就让AJAX摇摆不定而言,在jQuery中您说:

As far as getting the AJAX rocking, in jQuery you say:

$( function() {
  $('form.remote').submit( function() {
    var submitted_form = this;
    $.post( this.action, $.serialize(this), function( data_returned, status, request ) {
      var updated_block = $( data_returned ).find( $(submitted_form).attr('data-update').html();
      $( $(submitted_form).attr('data-update') ).html( updated_block );
    } );
    return false;
  } );

});

这使得每个表单都可以在发布后通过服务器上的新版本发布并更新其可更新块.您可以使用此元数据插件,并且可以使用元数据插件来存储有关应更新哪个块的信息以及有关请求的其他信息,但这很简单,可以让您在html中查看配置. data-x属性是HTML5的预定功能,但是我们仍然可以继续使用它们.

This allows each form to post and updates its updateable block with a new version from the server once a post has occurred. You can get fancier and use the metadata plugin to store info on which block should be updated and other info about the request, but this is simple and allows you to see the configuration in your html. data-x attributes are a scheduled feature of HTML5, but we can go ahead and use them anyway.

通过为您的远程表单创建约定,很容易使jQuery用少量代码处理所有ajax帖子.您可能会需要一些更高级的东西,微调器,验证等.有足够的空间,但这将使您从一个页面界面开始.

By creating a convention for your remote forms, it's easy to make jQuery handle all your ajax posts with a small amount of code. You'll probably want some fancier stuff, spinners, validations, etc. There's room for that, but this will get you started with a single page interface.

这篇关于(Ruby,Rails)CRUD在单个页面上嵌套了超过4个级别的模型...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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