I have a form_tag for sending an email and I want to use AJAX, so that only the form could update and the rest would look the same.
contact.html.erb
<%= form_tag(site_contact_path, remote: true, class: 'response form-horizontal', authenticity_token: true) do %>
<h2 class="contact-title">Parašykite mums</h2>
<div class="form-group">
<label class="control-label col-sm-4" for="name">Vardas</label>
<div class="col-sm-8">
<%= text_field_tag 'name', nil, placeholder: 'Įveskite vardą', class: 'form-control', autocomplete: 'on' %>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-4" for="email">El. paštas</label>
<div class="col-sm-8">
<%= text_field_tag 'email', nil, placeholder: 'Įveskite el.paštą', class: 'form-control', autocomplete: 'on' %>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-4" for="comment">Komentarai</label>
<div class="col-sm-8">
<%= text_area_tag 'comment', nil, placeholder: 'Jūsų komentaras', class: 'form-control', autocomplete: 'on', rows: '6' %>
</div>
</div>
<%= hidden_field_tag :authenticity_token, form_authenticity_token %>
<div class="button-holder">
<%= submit_tag 'Siųsti', class: 'submit' %>
</div>
<div class="hidden">
<p>Ačiū! (Mes pasistenksime atsakyti Jums, kuo greičiau)</p>
</div>
<% end %>
routes.rb
post '/contact' => 'site#contact_send_email'
site_controller.rb
def contact_send_email
@name = params[:name]
@email = params[:email]
@comment = params[:comment]
ContactMailer.send_message(@name, @email, @comment).deliver
respond_to do |format|
format.js
end
end
contact_send_email.js.erb
$('.form-group').hide();
$('.button-holder').hide();
$('.hidden').show();
log
ActionController::UnknownFormat (ActionController::UnknownFormat): app/controllers/site_controller.rb:24:in `contact_send_email'
When I press the submit button, the email is send but it seems contact_send_email action can't find contact_send_email.js.erb for some reason or doesn't understand format.js I've tried searching for similar problem on StackOverflow but the solutions didn't seem to make any effect.
Aucun commentaire:
Enregistrer un commentaire