保存具有所选ID的项目

时间:2017-04-17 12:47:09

标签: ruby-on-rails

我有一个“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”

.i执行compra.rb(has_many)和items.rb(belongs_to)中的关联

1 个答案:

答案 0 :(得分:0)

我认为你以错误的方式面对这个问题。你想要解决的只是一个主要的细节。请检查以下railscast,看看如何在rails中使用嵌套属性,只需一次操作即可保存master和所有细节:

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