0

我正在关注Ryan Bates 的 Railscast #136并尝试在提交表单后使用 AJAX 将 div ('#inviteform1')替换为部分('thankyou')。出于某种原因,当提交表单时,它不起作用——而是出现一个空白屏幕,并且 URL 重定向到 localhost:3000/inviterequests。我错过了什么?

邀请请求控制器

 def create
   @inviterequest = Inviterequest.new(params[:inviterequest])

   respond_to do |format|
    format.js
   end
 end

邀请请求/create.js.erb

$("#inviteform1").hide().after('<%= j render("thankyou") %>')

invokerequests/_form.html.erb -- 包含要替换的 div

<%= simple_form_for(@inviterequest, html: { class: 'form-horizontal'}) do |f| %>
  <% if @inviterequest.errors.any? %>
    <div id="error_explanation">
      <h3><%= pluralize(@inviterequest.errors.count, "error") %> prohibited this inviterequest from being saved:</h3>

    <ul>
      <% @inviterequest.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
    </ul>
  </div>
<% end %>

<!-- INVITE BOX CONTENT -->

<div class="invitebox">
  <div class="invitebox-inner">
    <div id="inviteform1">
     <br> <br> <br> 
      <h5> INDIAN WEDDING <BR> PLANNING JUST GOT EASIER </h5>  

      <%= image_tag("smalldivider.png") %>

      <h5> REQUEST AN INVITE </h5>  

      <div class="field">  
        <%= f.input :email, :placeholder => 'enter your email, please', label: false %>
      </div>

      <div class="actions">
        <%= f.submit "SUBMIT", class: "btn btn-primary btn-special", remote: true %>
      </div>
    </div>
</div>
</div>
<% end %>

invokerequests/_thankyou.html.erb -- 替换 div 的部分

<div id="thankyou">
<br> <br> <br>
<h3>THANK YOU </h3>
<h4> We are working hard to launch soon <br> and will be in touch. </h4>

<%= image_tag("smalldivider.png") %>

<p class="smallcaps"> <br> SHARE WITH FRIENDS IN THE MEANTIME </p>

    <div class="socialbuttons">
        <div class="twitter">
        <%= link_to (image_tag("twitter2.png")), "https://twitter.com/intent/tweet?screen_name=namastewedding&text=is%20making%20Indian%20wedding%20planning%20easy.%20I%20just%20requested%20my%20invitation.%20%23indianwedding%20www.namastewedding.com", :target => '_blank' %>

        </div>
        <div class="facebook">
        <%= link_to (image_tag("facebook2.png")), "https://www.facebook.com/pages/Namaste-Wedding/359192570873560" %>
        </div>
    </div>
</div>

日志

Started POST "/inviterequests" for 127.0.0.1 at 2013-08-29 22:08:39 -0400
Processing by InviterequestsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"pqDBzdaYKuOfjuVo1ovHvrQJTTrsUptAcQekCn4X+Ho=", "inviterequest"=>{"email"=>"foobar@gmail.com"}, "commit"=>"SUBMIT"}
Completed 406 Not Acceptable in 1ms (ActiveRecord: 0.0ms)
4

1 回答 1

2

看来您应该使用remote: trueinsimple_form_for而不是f.submit. 如

  <%= simple_form_for(@inviterequest, remote:true, html: { class: 'form-horizontal'}) do |f| %>
于 2013-08-30T02:32:05.090 回答