2017-04-17 106 views
0

我有一個「Compras」列表,每個「Compra」有很多文章。 當我選擇「Compra」重定向到Compra項目(has_meny) 我的問題是用Compra id保存文章。用選定的編號保存項目

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 

。在上述ItemsController我不知道如何來參考選擇 「Compra」

。我做的協會compra.rb(has_many)和items.rb(belongs_to)

+0

試試這個@ item.compra = current_compra –

回答

0

我認爲你面對的是問題是錯誤的。你想要解決的只是一個主 - 細節。請檢查下面的railscast,看看如何使用嵌套屬性在軌保存主機和所有的細節,在短短一個操作:

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

+0

儘量避免使用鏈接要回答,他們可能無法爲未來的讀者。 –

+0

謝謝塞巴斯蒂安!我是一個新手,我沒有考慮它。我將來會繼續考慮它。 –