Good afternoon, I need your help.
I need to show a success or rejection message, once I have inserted an object in BD. So far I have only managed to update the browser window, not automatically (as it should be), once the event submit has been triggered.
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 %>
Expected as an image:
Once the message is displayed, the idea is to show the start page again, after 1 second, without the message and the clean form.
Project structure is as follows.
creatives / index.html.erb
<%= render 'navbar' %>
<%= render 'header' %>
<%= render 'services' %>
<%= render 'portfolio' %>
<%= render 'call_to_action' %>
<%= render 'contact' %>
File quotes / new.html.erb
<h1>New Cotizacion</h1>
<%= render 'form', cotizacion: @cotizacion %>
<%= link_to 'Back', cotizacions_path %>
Routes.rb file
Rails.application.routes.draw do
get 'creatives/index'
root :to => 'creatives#index'
resources :cotizacions
post 'creatives/index', to: 'cotizacions#create'
end
Log file
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 ==", "quote" = > {"patent" = > "congetenriutes", "brand "= >" "," model "=" "," mileage "=" "," mail "=" "," phone "=" "," abs "= >" 0 "," air_conditioned "=>" 0 "," airbag "= >" 0 "," velocity_cruise "=>" 0 "}," commit "= &" Rate your Vehicle "} [1m [35m (0.1ms) [0m [1m [36mbegin transaction [0m [1m [35mSQL (0.4ms) [0m [1m [32mINSERT INTO "quotes" ("patent", "brand", "model", "mileage", "mail", "telephone", "abs", "air_conditioned" , "airbag", "speed_strike", "created_at", "updated_at") VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?) [0m [["patent" , "congetenriutes"], ["brand", ""], ["model", ""], ["mileage", ""], ["mail", ""], ["phone", ""] , ["abs", "f"], ["air_conditioned", "f"], ["airbag", "f"], ["speed_crucer", "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 link 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 quotes / _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)