Rails 5:事件系统

时间:2018-07-11 17:43:11

标签: ruby-on-rails ruby ruby-on-rails-5 associations

我正在制作一个事件系统。我有一个模型事件,每个事件都会有文件,报告,提供程序等。

今天是我的路线:

  scope 'app', :module => "app" do
    resources :events, :roteiros, 
              :convidados, :lista_convidados,
              :orcamentos, :orcamento_items, :providers, 
              :providers_items, :reports, :reports_items,
              :modelos, :modelos_items, :arquivos, :arquivos_items   
    end

我正在寻找其他模型的最佳方法,当编辑或创建新数据时,新信息与事件模型相关联,那么每个事件都将具有与之相关联的信息。

我认为应该是这样的:

scope 'app', :module => "app" do
    resources :events do

              :roteiros, 
              :convidados, :lista_convidados,
              :orcamentos, :orcamento_items, :providers, 
              :providers_items, :reports, :reports_items,
              :modelos, :modelos_items, :arquivos, :arquivos_items

    end
end

然后该网址看起来像/app/events/event-name/providers

我仍然需要获取事件ID,以便可以将新记录与特定事件相关联

所有模型都已经具有event_id关联值

谢谢您的帮助。

更新

  

当前路线:

  scope 'app', :module => "app" do
    resources :events do
      resources :arquivos, 
                :arquivos_items,
                :convidados, 
                :lista_convidados,
                :modelos, 
                :modelos_items, 
                :orcamento_items, 
                :orcamentos, 
                :providers, 
                :providers_items, 
                :reports, 
                :reports_items,
                :roteiros, shallow: true
    end

    resources :todo_lists do
      member do
        patch :complete
      end

      resources :todo_items do
        member do
          patch :complete
        end
      end
    end


  end
  

控制器

module App
  class OrcamentosController < SuperAppController
    before_action :set_orcamento, only: [:show, :edit, :update, :destroy]

    # GET /orcamentos
    # GET /orcamentos.json
    def index
      @orcamentos = Orcamento.all
      @orcamento = Orcamento.new
      @orcamento_item = OrcamentoItem.new
    end

    # GET /orcamentos/1
    # GET /orcamentos/1.json
    def show
    end

    # GET /orcamentos/new
    def new
      @orcamento = Orcamento.new
    end

    # GET /orcamentos/1/edit
    def edit
    end

    # POST /orcamentos
    # POST /orcamentos.json
    def create
      @orcamento = Orcamento.new(orcamento_params)

      respond_to do |format|
        if @orcamento.save
          format.html { redirect_to orcamentos_path, notice: 'Orcamento inserido.' }
          format.json { render :show, status: :created, location: @orcamento }
        else
          format.html { render :new }
          format.json { render json: @orcamento.errors, status: :unprocessable_entity }
        end
      end
    end

    # PATCH/PUT /orcamentos/1
    # PATCH/PUT /orcamentos/1.json
    def update
      respond_to do |format|
        if @orcamento.update(orcamento_params)
          format.html { redirect_to orcamentos_path, notice: 'Orcamento atualizado.' }
          format.json { render :show, status: :ok, location: @orcamento }
        else
          format.html { render :edit }
          format.json { render json: @orcamento.errors, status: :unprocessable_entity }
        end
      end
    end

    # DELETE /orcamentos/1
    # DELETE /orcamentos/1.json
    def destroy
      @orcamento.destroy
      respond_to do |format|
        format.html { redirect_to orcamentos_url, notice: 'Orcamento removido.' }
        format.json { head :no_content }
      end
    end

    private
      # Use callbacks to share common setup or constraints between actions.
      def set_orcamento
        @event = Event.friendly.find(params[:id])
        @orcamento = Orcamento.find(params[:id])
      end

      # Never trust parameters from the scary internet, only allow the white list through.
      def orcamento_params
        params.require(:orcamento).permit(:title, orcamento_items_attributes: [:id, :desc, :estimado, :real, :pago, :pendente, :vencimento, :orcamento_id, :venc, :_destroy])
      end
  end
end
  

URL测试

/app/events/teste-de-evento/orcamentos
  

部分

<ul>
  <li><a href="/app/todo_lists">
      <div class="itemMenu <%= 'active' if controller_name == 'todo_lists' %>">CHECKLIST</div>
    </a></li>
  <li><a href="<%= event_orcamentos_url(@event) %>">
      <div class="itemMenu <%= 'active' if controller_name == 'orcamentos' %>">ORÇAMENTO</div>
    </a></li>
  <li><a href="/app/lista_convidados">
      <div class="itemMenu <%= 'active' if controller_name == 'lista_convidados' %>">CONVIDADOS</div>
    </a></li>
  <li><a href="">
      <div class="itemMenu">AGENDA</div>
    </a></li>
  <li><a href="/app/providers/">
      <div class="itemMenu <%= 'active' if controller_name == 'providers' %>">FORNECEDORES</div>
    </a></li>
  <li><a href="/app/roteiros/">
      <div class="itemMenu <%= 'active' if controller_name == 'roteiros' %>">ROTEIROS</div>
    </a></li>
  <li><a href="/app/arquivos">
      <div class="itemMenu <%= 'active' if controller_name == 'arquivos' %>">ARQUIVOS</div>
    </a></li>
  <li><a href="/app/reports/">
      <div class="itemMenu <%= 'active' if controller_name == 'reports' %>">RELATÓRIOS</div>
    </a></li>
</ul>
  

查看

<section id="content">
    <section class="row wrapContent">
      <header class="sHeaderPagina column large-12 medium-12 small-12">
        <a href="javascript:history.go(-1);">
          <h2 class="backMenu"><div class="arrow"></div>Inicial</h2>
        </a>
        <!-- END BACK MENU -->

        <nav id="sMenuEventos">
          <%= render 'sharedApp/navegacao' %>

          <div class="acoesGerenciar">
            <ul>
              <li>
                <a href="">
                  <div class="AddUser"></div>
                </a>
              </li>

              <li>
                <a href="">
                  <div class="btPDF"></div>
                </a>
              </li>

              <li>
                <a href="/app/events/teste-evento/edit">
                  <div class="btConfigs"></div>
                </a>
              </li>
            </ul>
          </div>
        </nav>
        <!-- END NAV EVENTOS -->

        <div class="row">
          <div class="infoWidget column large-3 medium-6 small-12">
            <div class="boxInfo">
              <h3>Custo Estimado</h3>
              <h2>R$ 0,00</h2>
            </div>
          </div>
          <!-- END WIDGET -->
          <div class="infoWidget column large-3 medium-6 small-12">
            <div class="boxInfo">
              <h3>Custo Real</h3>
              <h2>R$ 0,00</h2>
            </div>
          </div>
          <!-- END WIDGET -->
          <div class="infoWidget column large-3 medium-6 small-12">
            <div class="boxInfo">
              <h3>Valor Pago</h3>
              <h2>R$ 0,00</h2>
            </div>
          </div>
          <!-- END WIDGET -->
          <div class="infoWidget column large-3 medium-6 small-12">
            <div class="boxInfo">
              <h3>Custo Pendente</h3>
              <h2>R$ 0,00</h2>
            </div>
          </div>
          <!-- END WIDGET -->
        </div>

      </header>
      <!-- END HEADER -->

      <a id="addItemBt" href="javascript:;"><span class="btAdd">+</span></a>

<% if @orcamentos.any? 
        @orcamentos.each do |orcamento| %>
      <article class="sTable sTableActive column large-12 medium-12 small-12">
        <a href="javascript:;" class="openTable">
          <header>
            <h3><%= orcamento.title %></h3>
            <div class="arrow"></div>
          </header>
        </a>

        <div class="wrapTable ">
          <table class="hover">
            <thead>
              <tr>
                <th width="200">Descrição</th>
                <th>Custo Estimado</th>
                <th width="150">Custo Real</th>
                <th width="150">Valor Pago</th>
                <th width="150">Valor Pendente</th>
                <th width="150">Próximo Vencimento</th>
              </tr>
            </thead>
            <tbody>

              <% orcamento.orcamento_items.each do |o| %>
                <tr>
                  <td><%= o.desc %></td>
                  <td>R$ <%= o.estimado %></td>
                  <td class="blue">R$ <%= o.real %></td>
                  <td>R$ <%= o.pago %></td>
                  <td>R$ <%= o.pendente %></td>
                  <td>
                    <%= o.vencimento %>
                  </td>
                </tr>
              <% end %>

              <tr>
                <%#= form_for(new_convidado_path) do |f| %>
                <%= form_for @orcamento_item, :url => { :controller => "orcamento_items", :action => "create" }, :html => {:method => :post} do |f| %>
                  <td>
                    <%= f.hidden_field :orcamento_id, value: orcamento.id, required: true %>

                    <%= f.telephone_field :desc, placeholder: "Descrição", required: true %>
                  </td>

                  <td>
                    <%= f.telephone_field :estimado, placeholder: "Estimado", required: true, class: "valor" %>
                  </td>

                  <td>
                    <%= f.telephone_field :real, placeholder: "Real", required: true, class: "valor" %>
                  </td>

                  <td>
                    <%= f.telephone_field :pago, placeholder: "Pago", required: true, class: "valor" %>

                  </td>

                  <td>
                    <%= f.telephone_field :pendente, placeholder: "Pendente", required: true, class: "valor" %>
                  </td>


                  <td>
                    <%= f.text_field :vencimento, placeholder: "Vencimento", required: true, class: "data" %>
                    <%= f.submit "+", class: "plusSubmit" %>
                  </td>

                <% end %>
              </tr>

              <tr class="total">
                <td class="subtotal">Sub-total</td>
                <td>R$ 0.000</td>
                <td class="blue">R$ 0.000</td>
                <td>R$ 0.000</td>
                <td>R$ 0.000</td>
                <td></td>
              </tr>

            </tbody>
          </table>

          <ul class="acoes">
            <% orcamento.orcamento_items.each do |c| %>
              <li>
                  <div class="btActions"></div>
                  <div class="opcoesCadastro">
                    <a href="">
                      <div class="editar"></div>
                    </a>
                    <a href="">
                      <div class="excluir"></div>
                    </a>
                  </div>
              </li>
              <!-- END ITEM -->
            <% end %>
          </ul>
        </div>

      </article>
      <!-- END LISTA -->
<% end else %>
  <article class="sTable sTableSelect sTableActive column large-12 medium-12 small-12">
        <a href="javascript:;" class="openTable">
          <header>
            <h3>Nenhuma orçamento ainda</h3>
            <div class="arrow"></div>
          </header>
        </a>
  </article>
<% end %>






    </section>
  </section>
<!-- ========================== END CONTENT -->


  <section class="modal modalCadastro" style="display: none;">
    <section class="boxModal centerModal">
      <header>
        <h2>Adicionar Tabela de Orçamento</h2>
        <a class="fecharBt fecharModal" href="javascript:;"><img src="./assets/images/icons/fechar.png" alt="" class="fechar"></a>
      </header>

      <%= form_with(model: @orcamento, local: true, html: { class: "row" }) do |f| %>
        <div class="inputFormCadastro">
          <label for="">Nome da lista</label>
          <%= f.text_field :title, required: true %>
        </div>
        <!-- END ITEM -->

        <h3 class="titleBoxExtras">Adicionar Orçamento</h3>
        <div id='boxCamposExtras'>
          <%= f.fields_for :orcamento_items do |task| %>
            <%= render 'orcamento_items_fields', :f => task %>
          <% end %>
          <div class='links'>
            <%= link_to_add_association 'adicionar orçamento', f, :orcamento_items %>
          </div>
        </div>



        <div class="botoes">
          <%= f.submit "CRIAR", class: "botao" %>
          <a href="#" class="btLink fecharModal">CANCELAR</a>
        </div>
      <% end %>

    </section>
  </section>
  <!-- END MODAL -->

enter image description here

1 个答案:

答案 0 :(得分:1)

首先,您需要在嵌套项目之前使用resources。像这样:

scope 'app', :module => "app" do
  resources :events do
    resources :roteiros, 
              :convidados, :lista_convidados,
              :orcamentos, :orcamento_items, :providers, 
              :providers_items, :reports, :reports_items,
              :modelos, :modelos_items, :arquivos, :arquivos_items
  end
end

当您在控制台中运行rake routes时,将为您提供以下信息:

    event_roteiros GET    /app/events/:event_id/roteiros(.:format)              app/roteiros#index
                   POST   /app/events/:event_id/roteiros(.:format)              app/roteiros#create
 new_event_roteiro GET    /app/events/:event_id/roteiros/new(.:format)          app/roteiros#new
edit_event_roteiro GET    /app/events/:event_id/roteiros/:id/edit(.:format)     app/roteiros#edit
     event_roteiro GET    /app/events/:event_id/roteiros/:id(.:format)          app/roteiros#show
                   PATCH  /app/events/:event_id/roteiros/:id(.:format)          app/roteiros#update
                   PUT    /app/events/:event_id/roteiros/:id(.:format)          app/roteiros#update
                   DELETE /app/events/:event_id/roteiros/:id(.:format)          app/roteiros#destroy

                   ... omitting a bunch of routes ...

            events GET    /app/events(.:format)                                 app/events#index
                   POST   /app/events(.:format)                                 app/events#create
         new_event GET    /app/events/new(.:format)                             app/events#new
        edit_event GET    /app/events/:id/edit(.:format)                        app/events#edit
             event GET    /app/events/:id(.:format)                             app/events#show
                   PATCH  /app/events/:id(.:format)                             app/events#update
                   PUT    /app/events/:id(.:format)                             app/events#update
                   DELETE /app/events/:id(.:format)                             app/events#destroy

如您所见,这将导致所有内容都在app下命名。这意味着您的控制器将需要如下所示:

class App::RoteirosController < ApplicationController 
  ...
end

该文件将位于:

app
 |- controllers
 |   |- app
 |   |   |- roteiros_controller.rb

我怀疑你不想这么做。因此,它应该更像是:

resources :events do
  resources :roteiros, 
            :convidados, :lista_convidados,
            :orcamentos, :orcamento_items, :providers, 
            :providers_items, :reports, :reports_items,
            :modelos, :modelos_items, :arquivos, :arquivos_items
end

docs in section 2.7.2 Shallow Nesting中所述,您可以考虑做类似的事情:

resources :events do
  resources :arquivos, 
            :arquivos_items,
            :convidados, 
            :lista_convidados,
            :modelos, 
            :modelos_items, 
            :orcamento_items, 
            :orcamentos, 
            :providers, 
            :providers_items, 
            :reports, 
            :reports_items,
            :roteiros, 
            shallow: true
end

在这种情况下,您会得到类似的东西:

    event_arquivos GET    /events/:event_id/arquivos(.:format)              arquivos#index
                   POST   /events/:event_id/arquivos(.:format)              arquivos#create
 new_event_arquivo GET    /events/:event_id/arquivos/new(.:format)          arquivos#new
      edit_arquivo GET    /arquivos/:id/edit(.:format)                      arquivos#edit
           arquivo GET    /arquivos/:id(.:format)                           arquivos#show
                   PATCH  /arquivos/:id(.:format)                           arquivos#update
                   PUT    /arquivos/:id(.:format)                           arquivos#update
                   DELETE /arquivos/:id(.:format)                           arquivos#destroy

                   ... omitting a bunch of routes ...

            events GET    /events(.:format)                                 events#index
                   POST   /events(.:format)                                 events#create
         new_event GET    /events/new(.:format)                             events#new
        edit_event GET    /events/:id/edit(.:format)                        events#edit
             event GET    /events/:id(.:format)                             events#show
                   PATCH  /events/:id(.:format)                             events#update
                   PUT    /events/:id(.:format)                             events#update
                   DELETE /events/:id(.:format)                             events#destroy

但是,根据您的示例,您似乎想使用:event_name作为标识符而不是:event_id。在这种情况下,您可以这样做:

resources :events, param: :name do
  resources :arquivos, 
            :arquivos_items,
            :convidados, 
            :lista_convidados,
            :modelos, 
            :modelos_items, 
            :orcamento_items, 
            :orcamentos, 
            :providers, 
            :providers_items, 
            :reports, 
            :reports_items,
            :roteiros, 
            shallow: true
end

哪个会给你:

    event_arquivos GET    /events/:event_name/arquivos(.:format)            arquivos#index
                   POST   /events/:event_name/arquivos(.:format)            arquivos#create
 new_event_arquivo GET    /events/:event_name/arquivos/new(.:format)        arquivos#new
      edit_arquivo GET    /arquivos/:id/edit(.:format)                      arquivos#edit
           arquivo GET    /arquivos/:id(.:format)                           arquivos#show
                   PATCH  /arquivos/:id(.:format)                           arquivos#update
                   PUT    /arquivos/:id(.:format)                           arquivos#update
                   DELETE /arquivos/:id(.:format)                           arquivos#destroy

                   ... omitting a bunch of routes ...

            events GET    /events(.:format)                                 events#index
                   POST   /events(.:format)                                 events#create
         new_event GET    /events/new(.:format)                             events#new
        edit_event GET    /events/:name/edit(.:format)                      events#edit
             event GET    /events/:name(.:format)                           events#show
                   PATCH  /events/:name(.:format)                           events#update
                   PUT    /events/:name(.:format)                           events#update
                   DELETE /events/:name(.:format)                           events#destroy

我想这就是您要寻找的。