javascript - 向 Rails 提交远程真实 form_for 并且不重定向

标签 javascript ruby-on-rails

我在网络开发方面确实是个新手。我几周前才开始阅读 Ruby on Rails 教程,在此之前我只了解一点 C+。所以这个问题对你来说可能很简单,但我已经花了两天时间寻找答案但没有找到。 我所拥有的是一个带有数据字段的 form_for ,该数据字段的出现/消失取决于单击的单选按钮。这工作正常,当我提交表单时,数据已正确保存在数据库中!问题是我根本没有被重定向。 shell中是这么写的

在布局/应用程序中渲染 Patient/show.html.erb 已完成 200 ok

但在浏览器中我仍然看到/srtinitial_infos/new (即我填写表单的页面)。

我的代码是

   #new.html.erb
<%= form_for(:srtinitial_infos, url: {action: "create"},remote: true) do |f|%>
<%= javascript_include_tag "srtinitial_infos_new.js" %>
 <p>
    <strong>Operations  </strong>
<%= f.radio_button :operation, '1', :value=>1%>
<%=label   :operation_yes,'Yes'%>
<%= f.radio_button   :operation, '0', :value=>0%>
<%=label   :operation_no,'No'%>
<%= f.radio_button   :operation, '2', :value=>2%>
<%=label   :operation_ni,'No info'%>
</p>    

<div id="opdate">
<p>
<strong>Date of operation  </strong>
<%=f.date_select :operationdate%><br>
<p>
</div>
<p>
<strong>Date of MRI  </strong>
<%=f.date_select :mridate%><br>
<p>


<%=f.submit "Save" %>

Javascript

 #javascript/srtinitial_infos_new.js.erb
 $(function(){

$("#opdate").hide();

$("input:radio[name='srtinitial_infos[operation]']").change(function(){  

        if(this.value == 1 && this.checked){
          $("#opdate").show();
        }else{
          $("#opdate").hide();
        }

    });

  });

Controller

def create
  @patient=Patient.find(params[:patient_id])
if @patient.srtinitial_info
 render 'show'
else
@srtinitial_info=@patient.create_srtinitial_info(srtinitial_infos_params)
 @srtinitial_info.author=@current_user.email
 @srtinitial_info.save
 redirect_to patient_path(@patient)
 end
end

由于我认为问题出在 Controller 中,因为数据保存正确,所以我也尝试编写类似的内容

   def create
@patient=Patient.find(params[:patient_id])
@srtinitial_info=@patient.create_srtinitial_info(srtinitial_infos_params)
@srtinitial_info.author=@current_user.email
respond_to do |format|
    if @patient.srtinitial_info
     format.html { render action: "show"}
    else @srtinitial_info.save
     format.html { redirect_to @patient, notice: 'Correctly initialized.'}
     format.js   {}
    end

我在新 Controller 中尝试了类似的东西

 def new
  @patient=Patient.find(params[:patient_id])
  respond_to do |format|
    format.html # new.html.erb  
    format.js
  end
 end

如果我不使用 Remote :true一切正常(显然除了隐藏/显示选项) 我希望问题很清楚:) 干杯

最佳答案

您可以使用.js.erb文件来响应AJAX请求。在这种情况下,您需要将 show.js.erb 放在与 show.html.erb 相同的 View 文件夹中。该文件使用 JavaScript 来更新页面,无需刷新/重定向。

假设您有一个 id 为 notice 的 div,您想在其中显示消息,您的 show.js.erb 可能如下所示:

$("#notice").html("<%= flash [:notice] %>"):

如果您想重定向到另一个页面,那么您可以从 Controller 执行客户端重定向:

window.location = "<%= patient_path @user %>"; 

关于javascript - 向 Rails 提交远程真实 form_for 并且不重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30352662/

相关文章:

ruby-on-rails - 具有相同 Time.zone、相同服务器时区但从 Date.to_time 获得不同结果的两个 Rails 服务器

javascript - 如何理解这些函数中的字节序 : 'writeFloatLE' , 'writeFloatBE' 、 'writeDoubleLE' 、 'writeDoubleBE'

javascript - 如何通过名称调用私有(private)函数

ruby-on-rails - Rails Controller 手动清理参数

ruby-on-rails - Searchkick/Rails 使用 Mongoid 的工作示例

javascript - 帮助通过 ror 通过 ajax 执行 javascript

ruby-on-rails - 单表继承在 Rails 4 应用程序中不起作用 - ActiveRecord::SubclassNotFound:单表继承机制失败

javascript - 跟踪传出 anchor 击到单独的帐户

javascript - 值更改时 ng-model 不更新

javascript - NodeJS - 将每个 HTTP 请求的 session 数据存储在 "global"变量中