[英]Rails Form_Tag Ajax Format.js ActionController::UnknownFormat
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. 我有一个form_tag用于发送电子邮件,我想使用AJAX,以便仅表单可以更新,其余的看起来相同。
contact.html.erb 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 的routes.rb
post '/contact' => 'site#contact_send_email'
site_controller.rb 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 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. 当我按下“提交”按钮时,电子邮件已发送,但由于某些原因或似乎不理解format.js,contact_send_email操作似乎找不到contact_send_email.js.erb,我曾尝试在StackOverflow上搜索类似的问题,但解决方案没有似乎没有任何效果。
In your form_tag, change the first parameter site_contact_path
to contact_path
. 在您的form_tag中,将第一个参数
site_contact_path
更改为contact_path
。 Also change the line on your routes to : 还将路线上的线更改为:
post '/contact' => 'site#contact_send_email', defaults: { format: 'js' }
This should fix your problem. 这应该可以解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.