Ruby on rails 使用所选id保存项目

Ruby on rails 使用所选id保存项目,ruby-on-rails,Ruby On Rails,我有一个“Compra”的列表,每个“Compra”都有很多文章。 当我选择“COMPA”时,重定向到COMPA的项目(有) 我的问题是用Compra id保存一篇文章 index.html.erb <h1 align = "center"><%= provide(:title, "Listado Pedidos de Compras") %></h1> <%= form_tag(compras_path, :method => "

我有一个“Compra”的列表,每个“Compra”都有很多文章。 当我选择“COMPA”时,重定向到COMPA的项目(有) 我的问题是用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>

“获取”,id:“搜索表单”)do%>
{:class=>“表格表格悬停表格边框表格条带”}do |表格|%>
“第二名”%>
“优先权”%>
“Fecha Pedido”%>
“Prioridad”%>
“Fecha Deseada”%>
“Fecha Limite”%>
“Motivo COMPA”%>
“特殊技术”%>
“Editar”,link_url=>lambda{| compra | edit_compra_path(compra)}%>
“删除”,“链接方法=>:删除”,“链接确认=>”是否继续
“Ver”,:link_url=>lambda{| compra | compra_路径(compra)}%>
“项目”,:link_url=>lambda{| compra | compra_项目_路径(compra)}%>
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
class ItemsControllerparams[:page],:per_page=>10)
结束
def新
@item=item.new
结束
def创建
@item=item.new(items\u参数)
@项目=当前项目
如果@item.save
flash[:success]=“Se realizóel pedido correctante”
重定向到压缩项路径(@item)
其他的
呈现“新”
结束
结束
私有的
def set_项目
@item=item.find(参数[:id])
结束
定义项目参数
参数require(:item).permit(:compa_id,:part_number,:description,:fabricante,:cantidad,:unidad)
结束
结束
.在itemsController中,我不知道如何引用选定的“Compra”


.我在compra.rb(有许多)和items.rb(属于)中进行关联。

我认为您以错误的方式面对问题。你想解决的只是一个主要的细节。请检查以下railscast,了解如何使用rails中的嵌套属性在一次操作中保存主控形状和所有详细信息:


试试这个@item.compra=current\u compatry为避免使用链接来回答问题,未来的读者可能无法使用这些链接。谢谢Sebastian!我是个新手,我没有考虑过。我以后会考虑的。