Buenas tardes, necesito de vuestra ayuda.
Necesito mostrar un mensaje de éxito o rechazo, una vez insertado un objeto en BD. Hasta el momento solo lo he logrado actualizando la ventana del navegador, no automáticamente (como debiera ser), una vez gatillado el evento submit.
cotizacions_controller.rb
def create
@cotizacion = Cotizacion.new(cotizacion_params)
if @cotizacion.save
flash[:success] = "Su tasación esta siendo procesada"
else
respond_to do |format|
format.html { render :new }
format.json { render json: @cotizacion.errors, status: :unprocessable_entity }
end
end
end
_header.html.erb
<header>
<div class="header-content">
<div class="header-content-inner">
<h1>Negocia tu Auto</h1>
<% flash.each do |key, value| %>
<div class="alert alert-success" role="alert">
<%= value %>
</div>
<% end %>
<%= render :partial =>'cotizacions/form' , locals: { cotizacion: @cotizacion } %>
</div>
</div>
</header>
_form.html.erb
<%= form_with(model: cotizacion, scope: :cotizacion) do |form| %>
<div class="container">
<div class="row">
<div class="col-md-2">
<div class="field">
<%= form.text_field :patente,
placeholder: "Patente",
id: :car_patente,
class: "form-control" %>
</div>
</div>
<div class="col-md-2">
<div class="form-inline">
<%= form.select :marca,
options_for_select([["SUBARU", "t"], ["KIA", "s"]], id: :car_marca),
prompt: "Marca",
class: "combobox form-control",
name: "inline"%>
</div>
</div>
<div class="col-md-2">
<div class="field">
<%= form.text_field :modelo,
id: :car_modelo,
placeholder: "Modelo Ej: Yaris, i10",
class: "form-control" %>
</div>
</div>
</div>
<!-- ... -->
<div class="row">
<div class="col-md-12 col-md-offset">
<div class="actions">
<%= form.submit class: 'btn btn-success btn-lg', value: 'Tasar su Vehículo' %>
</div>
</div>
</div>
</div>
<% end %>
Lo esperado como imagen:
La idea una vez mostrado el mensaje, es volver a mostrar la página de inicio, después de 1 segundo, sin el mensaje y el formulario limpio.
Estructura de proyecto es la siguiente.
creatives/index.html.erb
<%= render 'navbar' %>
<%= render 'header' %>
<%= render 'services' %>
<%= render 'portfolio' %>
<%= render 'call_to_action' %>
<%= render 'contact' %>
Archivo cotizacions/new.html.erb
<h1>New Cotizacion</h1>
<%= render 'form', cotizacion: @cotizacion %>
<%= link_to 'Back', cotizacions_path %>
Archivo routes.rb
Rails.application.routes.draw do
get 'creatives/index'
root :to => 'creatives#index'
resources :cotizacions
post 'creatives/index', to: 'cotizacions#create'
end
Archivo log
Started POST "/creatives/index" for 127.0.0.1 at 2017-08-01 10:00:45 -0400 Processing by CotizacionsController#create as JS Parameters: {"utf8"=>"✓", "authenticity_token"=>"6Y0r+8y7M5DJUeF7Gt2CziIEpMef4nLN8s6bvRA9jr+CHN2gRMGFaEzVgX/UnVc2YsCtNCH1DEs6cfi71r3PrA==", "cotizacion"=>{"patente"=>"congetenriutes", "marca"=>"", "modelo"=>"", "kilometraje"=>"", "correo"=>"", "telefono"=>"", "abs"=>"0", "aire_acondicionado"=>"0", "airbag"=>"0", "velocidad_crucero"=>"0"}, "commit"=>"Tasar su Vehículo"} [1m[35m (0.1ms)[0m [1m[36mbegin transaction[0m [1m[35mSQL (0.4ms)[0m [1m[32mINSERT INTO "cotizacions" ("patente", "marca", "modelo", "kilometraje", "correo", "telefono", "abs", "aire_acondicionado", "airbag", "velocidad_crucero", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)[0m [["patente", "congetenriutes"], ["marca", ""], ["modelo", ""], ["kilometraje", ""], ["correo", ""], ["telefono", ""], ["abs", "f"], ["aire_acondicionado", "f"], ["airbag", "f"], ["velocidad_crucero", "f"], ["created_at", "2017-08-01 14:00:45.399154"], ["updated_at", "2017-08-01 14:00:45.399154"]] [1m[35m (1.7ms)[0m [1m[36mcommit transaction[0m Redirected to http://localhost:3000/creatives/index Completed 200 OK in 29ms (ActiveRecord: 3.1ms)
Started GET "/" for 127.0.0.1 at 2017-08-01 10:00:47 -0400 Processing by CreativesController#index as HTML Rendering creatives/index.html.erb within layouts/creative Rendered creatives/_navbar.html.erb (0.3ms) Rendered cotizacions/_form.html.erb (2.6ms) Rendered creatives/_header.html.erb (4.3ms) Rendered creatives/_services.html.erb (0.4ms) Rendered creatives/_portfolio.html.erb (4.8ms) Rendered creatives/_call_to_action.html.erb (0.3ms) Rendered creatives/_contact.html.erb (0.4ms) Rendered creatives/index.html.erb within layouts/creative (18.2ms) Completed 200 OK in 50ms (Views: 48.8ms | ActiveRecord: 0.0ms)