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:
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