【问题标题】:Can I use resource routes without a database (without ids)我可以在没有数据库的情况下使用资源路由吗(没有 ID)
【发布时间】:2013-09-29 03:27:04
【问题描述】:

我正在构建一个没有模型、没有数据库的 UI 应用程序。它对所有业务逻辑使用 Web 服务调用。我在完成路由时遇到了难以置信的困难。我还有像workstations 这样的实体。我在routes resource :workstations 中声明了这些路线:

     workstations POST   /workstations(.:format)      workstations#create
 new_workstations GET    /workstations/new(.:format)  workstations#new
edit_workstations GET    /workstations/edit(.:format) workstations#edit
                  GET    /workstations(.:format)      workstations#show
                  PUT    /workstations(.:format)      workstations#update
                  DELETE /workstations(.:format)      workstations#destroy

但是当我尝试链接到像= link_to "Update", workstations_path, :method => :put 这样的更新操作时,我得到No route matches {:controller=>"workstations", :method=>"put"}。那不是为更新操作列出的路线吗?

我想知道我是否不能使用传统的资源丰富的路线,因为我没有ids。或者也许我只是做错了。

查看代码(Haml):

%h2
  %small#application-name-label Technical Support Interface
.col-md-3
  %h3 Workstations
  %button.btn.btn-success.create-button Create Workstation
  - if @workstations.blank?
    .no-items-available There are no workstations to display
  -else
    = form_tag(controller: "workstations", action: "delete_history_and_queue", method: "post") do
      %table#workstation-table.table.table-striped.table-hover.table-bordered
        %thead
          %tr
            %th#table-header Name
            %th#table-header Delete History
            %th#table-header Delete Queue
            %th#table-header Update
            %th#table-header Delete
        %tbody
          - @workstations.each do |workstation|
            %tr
              %td
                = workstation[:name]
              %td
                = check_box_tag 'delete history', form_class: "checkbox"
              %td
                = check_box_tag 'delete queue', form_class: "checkbox"
              %td
                = link_to "Update", workstations_path, :action => :update
              %td
                = link_to "Delete", workstations_path, :action => :destroy
    = submit_tag
.col-md-3
  %h3 Data Sources
  %button.btn.btn-success.create-button Create Data Source
  - if @data_sources.blank?
    .no-items-available There are no sources to display
  -else
    %table.table.table-striped.table-hover.table-bordered
      %tr
        %th#table-header Type
        %th#table-header Name
      - @data_sources.each do |data_source|
        %tr
          %td
            = data_source[:type]
          %td
            = data_source[:name]

HTML

<body>
    <div class='whole-page'>
      <div class='container'>
        <h1 class='hero-unit' id='application-title'>
          <div class='row-fluid'>
            <div class='span1' id='replication-server'>
              <img alt="Cog_logo" src="/assets/cog_logo.png" />
              <img alt="Crs" src="/assets/crs.png" />
              Replication Server
            </div>
          </div>
        </h1>
      </div>
    </div>
    <h2>
      <small id='application-name-label'>Technical Support Interface</small>
    </h2>
    <div class='col-md-3'>
      <h3>Workstations</h3>
      <button class='btn btn-success create-button'>Create Workstation</button>
      <form accept-charset="UTF-8" action="/?method=post" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="NQghiooSVtSngRU7K170uxhqJ2N6i9XQ6sP5W+UeKrY=" /></div>
        <table class='table table-striped table-hover table-bordered' id='workstation-table'>
          <thead>
            <tr>
              <th id='table-header'>Name</th>
              <th id='table-header'>Delete History</th>
              <th id='table-header'>Delete Queue</th>
              <th id='table-header'>Update</th>
              <th id='table-header'>Delete</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>
                consequatur
              </td>
              <td>
                <input id="delete_history" name="delete history" type="checkbox" value="{:form_class=&gt;&quot;checkbox&quot;}" />
              </td>
              <td>
                <input id="delete_queue" name="delete queue" type="checkbox" value="{:form_class=&gt;&quot;checkbox&quot;}" />
              </td>
              <td>
                <a href="/workstations" action="update">Update</a>
              </td>
              <td>
                <a href="/workstations" action="destroy">Delete</a>
              </td>
            </tr>
            <tr>
              <td>
                quia
              </td>
              <td>
                <input id="delete_history" name="delete history" type="checkbox" value="{:form_class=&gt;&quot;checkbox&quot;}" />
              </td>
              <td>
                <input id="delete_queue" name="delete queue" type="checkbox" value="{:form_class=&gt;&quot;checkbox&quot;}" />
              </td>
              <td>
                <a href="/workstations" action="update">Update</a>
              </td>
              <td>
                <a href="/workstations" action="destroy">Delete</a>
              </td>
            </tr>
            <tr>
              <td>
                quod
              </td>
              <td>
                <input id="delete_history" name="delete history" type="checkbox" value="{:form_class=&gt;&quot;checkbox&quot;}" />
              </td>
              <td>
                <input id="delete_queue" name="delete queue" type="checkbox" value="{:form_class=&gt;&quot;checkbox&quot;}" />
              </td>
              <td>
                <a href="/workstations" action="update">Update</a>
              </td>
              <td>
                <a href="/workstations" action="destroy">Delete</a>
              </td>
            </tr>
            <tr>
              <td>
                reprehenderit
              </td>
              <td>
                <input id="delete_history" name="delete history" type="checkbox" value="{:form_class=&gt;&quot;checkbox&quot;}" />
              </td>
              <td>
                <input id="delete_queue" name="delete queue" type="checkbox" value="{:form_class=&gt;&quot;checkbox&quot;}" />
              </td>
              <td>
                <a href="/workstations" action="update">Update</a>
              </td>
              <td>
                <a href="/workstations" action="destroy">Delete</a>
              </td>
            </tr>
            <tr>
              <td>
                laborum
              </td>
              <td>
                <input id="delete_history" name="delete history" type="checkbox" value="{:form_class=&gt;&quot;checkbox&quot;}" />
              </td>
              <td>
                <input id="delete_queue" name="delete queue" type="checkbox" value="{:form_class=&gt;&quot;checkbox&quot;}" />
              </td>
              <td>
                <a href="/workstations" action="update">Update</a>
              </td>
              <td>
                <a href="/workstations" action="destroy">Delete</a>
              </td>
            </tr>
            <tr>
              <td>
                quidem
              </td>
              <td>
                <input id="delete_history" name="delete history" type="checkbox" value="{:form_class=&gt;&quot;checkbox&quot;}" />
              </td>
              <td>
                <input id="delete_queue" name="delete queue" type="checkbox" value="{:form_class=&gt;&quot;checkbox&quot;}" />
              </td>
              <td>
                <a href="/workstations" action="update">Update</a>
              </td>
              <td>
                <a href="/workstations" action="destroy">Delete</a>
              </td>
            </tr>
            <tr>
              <td>
                laboriosam
              </td>
              <td>
                <input id="delete_history" name="delete history" type="checkbox" value="{:form_class=&gt;&quot;checkbox&quot;}" />
              </td>
              <td>
                <input id="delete_queue" name="delete queue" type="checkbox" value="{:form_class=&gt;&quot;checkbox&quot;}" />
              </td>
              <td>
                <a href="/workstations" action="update">Update</a>
              </td>
              <td>
                <a href="/workstations" action="destroy">Delete</a>
              </td>
            </tr>
            <tr>
              <td>
                non
              </td>
              <td>
                <input id="delete_history" name="delete history" type="checkbox" value="{:form_class=&gt;&quot;checkbox&quot;}" />
              </td>
              <td>
                <input id="delete_queue" name="delete queue" type="checkbox" value="{:form_class=&gt;&quot;checkbox&quot;}" />
              </td>
              <td>
                <a href="/workstations" action="update">Update</a>
              </td>
              <td>
                <a href="/workstations" action="destroy">Delete</a>
              </td>
            </tr>
          </tbody>
        </table>
      </form>
      <input name="commit" type="submit" value="Save changes" />
    </div>
    <div class='col-md-3'>
      <h3>Data Sources</h3>
      <button class='btn btn-success create-button'>Create Data Source</button>
      <table class='table table-striped table-hover table-bordered'>
        <tr>
          <th id='table-header'>Type</th>
          <th id='table-header'>Name</th>
        </tr>
        <tr>
          <td>
            CIDNE
          </td>
          <td>
            http://block.com/keaton_baumbach
          </td>
        </tr>
        <tr>
          <td>
            DCGS
          </td>
          <td>
            http://wolff.info/jace_rice
          </td>
        </tr>
      </table>
    </div>
  </body>
</html>

【问题讨论】:

    标签: ruby-on-rails ruby view controller routing


    【解决方案1】:

    您当然可以使用不带模型对象或 ID 的宁静路由。你所拥有的一切似乎都很好,我什至在本地尝试过,工作正常。

    如果您正在使用 rails s 进行测试,您可能需要重新启动,我过去曾遇到过重新启动修复的路由问题。

    【讨论】:

    • 好的,现在我得到The action 'show' could not be found for WorkstationsController,我猜这是进步。我不需要show 操作,所以我不确定它来自哪里。只是为了理智,您没有必须仅仅因为定义了路线而实施一个动作,还是你?我的猜测是它误解了workstations_path,但这似乎不对。
    • 如果你在/workstations 上尝试GET,你会得到那个错误,并且没有控制器方法来处理它。如果您不想要它,可以将其从生成的路由中排除,如下所示:resource :workstations, except: [:show]
    • 我输入了except,现在我得到No route matches [GET] "/workstations"
    • 我没有在任何地方打电话给get,所以我不明白。如果有帮助,我将发布我的视图代码
    • 您所有的链接:&lt;a href="/workstations" action="update"&gt;Update&lt;/a&gt; 都是 GET。我认为您仅在明确指定 controller 时才指定 action: 'whatever' 。由于您在 link_to 中使用 URL 帮助程序,因此您需要指定 method: :deletemethod: :put
    猜你喜欢
    • 1970-01-01
    • 2010-11-23
    • 1970-01-01
    • 1970-01-01
    • 2021-10-01
    • 1970-01-01
    • 2018-05-22
    • 2021-07-14
    • 2017-12-31
    相关资源
    最近更新 更多