我如何使一个页面ror网站与所有crud功能一起工作?

时间:2017-06-19 21:26:43

标签: ruby-on-rails ruby single-page-application crud

所以有一点这一切都有效,然后当我回来工作时,它停止了工作。我得到一个没有路由匹配错误的索引页

当我检查我的路线时,我觉得它们是有限的,但我不知道它是否与我想要做的事有关。
这来自localhost3000 / routes -

admin_packing_printers_path GET     /admin/packing_printers(.:format)       admin/packing_printers#index
                            POST    /admin/packing_printers(.:format)       admin/packing_printers#create
admin_packing_printer_path  PATCH   /admin/packing_printers/:id(.:format)   admin/packing_printers#update
                            PUT     /admin/packing_printers/:id(.:format)   admin/packing_printers#update
                            DELETE  /admin/packing_printers/:id(.:format)   admin/packing_printers#destroy

有趣的是我从rake路线或来自routes.rb找不到任何其他东西,但他们曾经也在那里。

指数 -

<% content_for(:meta_title) { 'Packing Printers' } %>

<% content_for(:title) do %>
    <div class='admin-sub-header'>
      <%= subheader_title('Packing Printers', icon: 'users') %>
    </div>
<% end %>
<div>
  <% if current_admin_user.manager %>
    <%= button_to("+ New Packing Printer", new_admin_packing_printer_path, {:class => 'btn btn-success', :method => :get}) %>
  <% end %>
</div>
<div class="widget-box">
  <%= widget_title('Packing Printers', icon: 'wrench') %>
  <div class="widget-content nopadding">
    <table class="table table-bordered table-striped table-hover data-table">
      <thread>
        <tr>
          <th>Name</th>
          <th>GUID</th>
          <th>Actions</th>
        </tr>
      </thread>
      <tbody>
        <% @printer = PackingPrinter.new %>
        <%= simple_form_for [:admin, @printer], method: puts do |f| %>
          <td><%= f.text_field(:name) %></td>
          <td><%= f.text_field(:guid) %></td>
          <td><%= button_to("Add New Printer", new_admin_packing_printer_path(@printer), {:class => 'btn btn-primary'}) %></td>
       <% end %>
        <tr>
          <% @printers.each do |printer| %>
          <%= simple_form_for [:admin, printer] do |f| %>
            <td><%= f.text_field(:name) %></td>
            <td><%= f.text_field(:guid) %></td>
            <td><%= button_to("Update", edit_admin_packing_printer_path(printer), {:class => 'btn btn-success', :method => puts}) %>
            <%= button_to("Delete", admin_packing_printer_path(printer),
            {:class => 'btn btn-danger', method: :delete, data: { confirm: 'Are you sure?'  }}) %>
            </td>
        <% end %>
      </tr>
      <% end %>
      </tbody>
    </table>
  </div>
</div>

控制器 -

class Admin::PackingPrintersController < AdminController
  def index
    @printers = PackingPrinter.all
  end

  def update
    @printer = PackingPrinter.find(params[:id])
    begin
      @printer.update!(packing_printer_params) if @printer.present?
    rescue => e
      flash[:errors] = "#{e}"
    end
    redirect_to action: "index"
  end

  def create
    begin
      PackingPrinter.create!(name: packing_printer_params[:name], guid: packing_printer_params[:guid])
    rescue => e
      flash[:errors] = "#{e}"
    end
    redirect_to action: "index"
  end

  def destroy
    @printer = PackingPrinter.find(packing_printer_params[:id])
    @printer.destroy
    if @printer.id?
      flash[:success] = 'Printer was deleted successfully.'
    else
      flash[:error] = 'Printer was not deleted'
    end
    redirect_to action: "index"
  end

private

  def packing_printer_params
    key = params[:packing_printer] if params.keys.include?('packing_printer')
    key ||= params
    key.permit(:id, :name, :guid)
  end

end

1 个答案:

答案 0 :(得分:0)

我忘了我已经删除了routes.rb中的一些资源,我需要关注 -

resources :packing_printers, only: [:index, :edit, :create, :new, :update, :destroy]