collect data from an index.htm.erb scaffold in ruby and take them to another scaffold index.html through a link

0

This is the code of index.html.erb of my scaffold contactos :

<h1>Contactos</h1>
<br>

<table class= "table-hover">
  <thead>
    <tr>
      <th>Nombre</th>
      <th>Apellidos</th>
      <th>Empresa</th>
 <th>ID</th>

      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @contactos.each do |contacto| %>
      <tr>
        <td><%= contacto.nombre %></td>
        <td><%= contacto.apellidos %></td>
        <td><%= contacto.empresa %></td>
        <td><%= link_to 'Show', contacto %></td>
        <td><%= link_to 'Edit', edit_contacto_path(contacto) %></td>
        <td><%= link_to 'Destroy', contacto, method: :delete, data: { confirm: 'Are you sure?' } %></td> 
        <td><%= link_to 'Negociacions', new_negociacion_path(contacto.nombre),  data: { confirm: 'Quiere añadir una negociacion para el contacto: '+(contacto.empresa)+'?'}%></td> 

It has several links: show , edit , destroy and negociacion . When I click on negociacion it takes me to "new negotiation", I would like you to take the contact data and take it directly to negotiation. All this because what I want is to create a new negotiation with the data of a contact, and since it would be very expensive to remember the name, surnames and company, I think it is better if I picked them up and sent them directly.

With new_negociacion_path(contacto.nombre) that has the negotiation link, what I get is that I actually take the name of the contact and show it to me when new negociacion opens, and I see it in the browser like this:

link

How could it be that in this case the name of Lucas (also surnames and company) put me directly in the fields of new negociacion : name, last name, company?

This is the contact controller code:

class ContactosController < ApplicationController
  before_action :set_contacto, only: [:show, :edit, :update, :destroy]
  layout "bienvenida" 

 # GET /contactos
  # GET /contactos.json
  def index
    @contactos = Contacto.all.paginate(page: params[:page], per_page:10)
  end

  # GET /contactos/1
  # GET /contactos/1.json
  def show
  end

  # GET /contactos/new
  def new
    @contacto = Contacto.new
  end

  # GET /contactos/1/edit
  def edit
  end

  # POST /contactos
  # POST /contactos.json
  def create
    @contacto = Contacto.new(contacto_params)

    respond_to do |format|
      if @contacto.save
        format.html { redirect_to @contacto, notice: 'Contacto was successfully created.' }
        format.json { render :show, status: :created, location: @contacto }
      else
        format.html { render :new }
        format.json { render json: @contacto.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /contactos/1
  # PATCH/PUT /contactos/1.json
  def update
    respond_to do |format|
      if @contacto.update(contacto_params)
        format.html { redirect_to @contacto, notice: 'Contacto was successfully updated.' }
        format.json { render :show, status: :ok, location: @contacto }
      else
        format.html { render :edit }
        format.json { render json: @contacto.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /contactos/1
  # DELETE /contactos/1.json
  def destroy
    @contacto.destroy
    respond_to do |format|
      format.html { redirect_to contactos_url, notice: 'Contacto was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_contacto
      @contacto = Contacto.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def contacto_params
      params.require(:contacto).permit(:nombre, :apellidos, :edad, :empresa, :domicilio, :localidad, :provincia, :pais, :telefono, :movil, :email, :foto, :descripcion, :notas, :cargo, :sitioweb, :agente_id)
    end
end 

and the contact model:

class Contacto < ApplicationRecord
    mount_uploader :foto, FotoUploader
    validates :nombre, presence: true
    has_many :negociacions
    has_many :agente_comercials
    has_many :tareas
    has_many :sales
    has_one :cliente
end
    
asked by user87071 07.06.2018 в 13:22
source

2 answers

0

The best thing you can do (from the REST point of view) is to generate a route like recommends mariovzc ; you can do this by nesting routes in your routes.rb file:

# config/routes.rb

Rails.application.routes.draw do
  resources :contactos do
      resources :negociaciones
  end
end

The above will generate, among others, the following route:

new_contacto_negociacione GET    /contactos/:contacto_id/negociaciones/new(.:format)               negociaciones#new

Then, in your view, use the route in this way:

<!-- app/views/contactos/index.html.erb  -->

<% @contactos.each do |contacto| %>
  <!-- otros datos de contacto -->
  <%= link_to 'Negociacion', new_contacto_negociacione_path(contacto),  data: { confirm: 'Quiere añadir una negociacion para el contacto: '+(contacto.empresa)+'?'}%>
<% end %>

And, finally, on your controller:

class NegociacionesController < ApplicationController
  # otras acciones

  def new
    @negociacion = Negociacion.new(contacto_id: params[:contacto_id])
  end
end

If you create the negocacion directly (i.e, without contacto_id ) it will be equivalent to just writing Negociacion.new ; but if you have contacto_id , then this will be assigned to negocacion .

This way you can use @negociacion.contacto in the view new.html.erb to show the contact's data; for example:

<!-- app/views/negociaciones/new.html.erb -->

<%= @negociacion.contacto.nombre %>
<%= @negociacion.contacto.empresa %>
    
answered by 09.06.2018 / 01:02
source
0

Sorry for the delay in answering. More or less I catch you where you go and what you suggest. Thank you. I commented to you following the steps that you indicate: in <% @ contacts.each do | contact | % >, I get a horizontal line with the word Negotiation as many times as I have contacts. However correct it is in vertical, next to each contact. Any suggestions to order them? And in Negociacionescontroller I do not know why I get an error: @negociacion = contacto.negociaciones.build, I've been reviewing and modifying it, but it does not stop coming, I think due to the build. Is there a possibility to send you the code? thanks.

    
answered by 14.06.2018 в 20:41