2017-04-17 5 views
0

Ich habe eine Liste von "Compras", und jede "Compra" hat viele Artikel. Wenn ich eine "Compra" Redirect zu Items of Compra (has_meny) Mein Problem ist, einen Artikel mit Compra-ID speichern.Artikel mit ausgewählter ID speichern

index.html.erb

<h1 align = "center"><%= provide(:title, "Listado Pedidos de Compras") %></h1> 




<%= form_tag(compras_path, :method => "get", id: "search-form") do %> 
<%= text_field_tag :search, params[:search], placeholder: "Buscar" %> 
<%= submit_tag "Buscar" %> 
<% end %> 


<%= link_to "Nuevo Pedido", new_compra_path, class:"btn btn-default" %> 

<%= table_for @compra, :table_html => { :class => "table table-hover table-bordered table-striped" } do |table| %> 
    <% table.column :numero_pedido, :header => "Numero Pedido" %> 
    <% table.column :numero_expediente, :header => "Numero Expediente" %> 
    <% table.column :created_at, :header => "Fecha Pedido" %> 
    <% table.column :prioridad, :header => "Prioridad" %> 
    <% table.column :fecha_deseada, :header => "Fecha Deseada" %> 
    <% table.column :fecha_limite, :header => "Fecha Limite" %> 
    <% table.column :motivo_compra, :header => "Motivo Compra" %> 
    <% table.column :especificacion_tecnica, :header => "Especificacion Tecnica" %> 
    <% table.column :data => "Editar", :link_url => lambda {|compra| edit_compra_path(compra) } %> 
    <% table.column :data => "Delete", :link_method => :delete, :link_confirm => "Estas seguro que quieres eliminar este pedido?" %> 
    <% table.column :data => "Ver", :link_url => lambda {|compra| compra_path(compra) } %> 
    <% table.column :data => "Items", :link_url => lambda {|compra| compra_items_path(compra) } %><!--link to items of Compra--> 


    <% table.footer do %> 
    <div class="pull-right"> 
     <div class="digg_pagination"> 
     <div class="page_info"> 

     </div> 

     </div> 
    </div> 
    <% end %> 
<% end %> 
    <div> 
<div> 

ItemsController.rb

class ItemsController < ApplicationController 
    before_action :set_item, only: [:edit, :update, :show, :destroy] 
    def index 
     @item = Item.all 
     @compra = Compra.all 
     @item = Item.where(pedidos_id: params[:id]) if params[:id].present? 
     @item = @item.search(params[:search]) if params[:search].present? 
     @item = @item.paginate(:page => params[:page], :per_page => 10) 
    end 
    def new 
    @item = Item.new 
    end 
    def create 
     @item = Item.new(items_params) 
     @item = current_compra 
     if @item.save 
      flash[:success] = "Se realizó el pedido correctamente" 
      redirect_to compra_item_path(@item) 
     else 
      render 'new' 
     end 
    end 

    private 
     def set_item 
      @item = Item.find(params[:id]) 

     end 
     def items_params 
      params.require(:item).permit(:compra_id, :part_number, :description, :fabricante, :cantidad, :unidad ) 
     end 

end 

.In itemsController ich weiß nicht, wie man ausgewählte "Compra"

.i die Verbände tun verweisen in compra.rb (has_many) und items.rb (gehört zu)

+0

Versuchen @ diese item.compra = current_compra –

Antwort

0

Ich denke, dass Sie th konfrontiert sind Das Problem ist falsch. Was Sie zu lösen versuchen, ist nichts anderes als ein Meister-Detail. Bitte überprüfen Sie folgende Railscast und sehen, wie verschachtelte Attribute in Schienen verwenden, um den Master und alle Details nur einen Betrieb in sparen:

http://railscasts.com/episodes/196-nested-model-form-part-1

+0

Versuchen Sie es mit Links zu vermeiden um zu antworten, könnten sie für zukünftige Leser nicht verfügbar sein. –

+0

Danke Sebastian! Ich bin ein Neuling, und ich habe es nicht berücksichtigt. Ich werde es in Zukunft berücksichtigen. –

Verwandte Themen