【问题标题】:Rails 5: Event systemRails 5:事件系统
【发布时间】:2018-07-11 17:43:10
【问题描述】:

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

这是我今天的路线:

  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

我正在寻找其他模型的最佳方式,当编辑或创建新数据时,新信息与 Event 模型相关联,然后每个事件都会有与之相关联的信息。

我认为应该是这样的:

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

网址测试

/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 -->

【问题讨论】:

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


    【解决方案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
    

    我认为这就是您正在寻找的。​​p>

    【讨论】:

    • 你好。非常感谢您的帮助。如您所说,路线如何工作。但是当添加到项目的链接时,输入 event_orcamentos_url 我得到错误:没有路由匹配 {:action=>"index", :controller=>"app/orcamentos", :id=>"teste-de-evento"} ,缺少必需的键:[:event_id]。我应该更改控制器中的某些内容以获取事件的 id 吗?
    • 我的控制器只获取特定项目的 id,例如 def set_orcamento @orcamento = Orcamento.find(params[:id]) end
    • 我尝试使用 event_orcamentos_url(@event) 并且它有效,但是当我转到链接时,我得到 No route matches {:action=>"index", :controller=>"app/orcamentos" , :event_id=>nil},缺少必需的键:[:event_id] 在路径中:
    • 你应该使用event_orcamentos_path(@event)。然后,您需要确保在控制器操作中设置了您的 @event 实例。看起来你可能没有设置@event,这就是你得到:event_id=&gt;nil的原因。
    • 另外,您需要决定是使用:id 还是:name 作为您的标识符。正如您在此处看到的:No route matches {:action=&gt;"index", :controller=&gt;"app/orcamentos", :id=&gt;"teste-de-evento"},看起来您正在尝试使用 name 作为 id
    猜你喜欢
    • 2012-09-22
    • 2011-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-08
    • 2011-10-09
    • 2012-09-01
    • 2018-09-18
    相关资源
    最近更新 更多