0

I'm a bit new to rails and I'm just trying to display a confirmation message when a "Proposition" (created model) has been inserted. For this, I'm using a javascript functionality embedded to rails. Unfortunately, when I click and submission button, the proposition is correctly inserted but I can't find a way to display this confirmation message (like "proposition correctly submitted" for example)...

Below the code that I'm using :

Proposition Model :

class Proposition < ActiveRecord::Base
belongs_to :ad
attr_accessible :email, :name, :phone, :price
validates_presence_of :name or :price or :email or :phone
end

Create method in controller :

def create
@ad = Ad.find(params[:ad_id])
@proposition = @ad.propositions.create(params[:proposition])

respond_to do |format|
if @proposition.save
format.html { redirect_to ad_path(@ad), notice: 'Proposition was successfully created.' }
format.json { render json: ad_path(@ad), status: :created, location: @proposition }
format.js 
else
format.html { render action: "new" }
format.json { render json: @proposition.errors, status: :unprocessable_entity }
format.js { render action: "new" }
end
end
end

_form.html.erb :

<%= form_for [@ad, @ad.propositions.build], :remote => true do |f| %>
<% if @proposition.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@proposition.errors.count, "error") %> prohibited this proposition from being saved:</h2>
<ul>
<% @proposition.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :Prénom %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :"Adresse mail" %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :Téléphone %><br />
<%= f.text_field :phone %>
</div>
<div class="field">
<%= f.label :"Prix proposé" %><br />
<%= f.text_field :price %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>

create.js.erb :

$('#new_proposition').fadeOut(1000);

new.js.erb :

$('#new_proposition_link').hide().after('<%= j render("form") %>');

Thanks for your help !

1 Answer 1

1

Since you already have a create.js.erb file, you can use JS to display an alert message or insert a confirmation message into the page like so,

# app/views/.../create.js.erb

$('#new_proposition').before('<p>Proposition correctly submitted!</p>');
$('#new_proposition').fadeOut(1000);

This inserts the <p>Proposition correctly submitted!</p> before the #new_proposition element. You may want to insert it somewhere else since this element will disappear.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.