【问题标题】:Rendering a js.erb file during form submission ajax Rails在表单提交期间呈现 js.erb 文件 ajax Rails
【发布时间】:2017-10-19 05:36:01
【问题描述】:

我有一个如下所示的表单:

<div class= "parent-container">
  <%= form_with scope: @company, url: companies_path, html: { class: "form-inline", remote: true, "data-type" => :js, id: "new-company-create" }, local: true do |f| %>
    <div class= "form-group">
      <%= f.label :nil, 'Company Name:', :class => 'sr-only'%>
      <%= f.text_field :nil, :class => 'form-control-plaintext' %>
    </div> 
    <div class="form-group mx-sm-3"> 
      <%= f.label :name, 'Enter a Company Name :   ' %> 
      <%= f.text_field :name, :class => 'form-control large-input-grp', :placeholder => "Enter any Company Name" %>     
    </div>
    <%= button_to "Create a Company", companies_path, class: "btn btn-default", id: "create_company_main", :type => "submit", :method => "post"%>
  <% end %>
</div>

我正在尝试通过 ajax Rails 提交并意识到一个简单的过程可能是多么痛苦。

在我的控制器中,表单发布到此方法:

def create
    @newCompany = Company.new(company_params) 
    respond_to do |format|
    if @newCompany.save
      format.js
      format.html { render :nothing => true, :notice => 'Company created successfully!' }
      format.json { render json: @newCompany, status: :created, location: @newCompany }
    else
      format.html { render action: "new" }
      format.json { render json: @newCompany, status: :unprocessable_entity }
    end
    end     
end

我在respond_to do |format| 块中尝试了多种组合,但似乎没有任何效果。

我所做的一切似乎都在返回 _create.js.erb 文件而不是寻找 HTML。

在我的终端中,我可以看到在发布请求期间显示以下内容:

Processing by CompaniesController#create as HTML

我可以看到的有关这方面的教程都已过时,我正在尝试逐步了解 Rails,但我被困在这样基本的东西上。

不明白为什么我必须做format.htmlformat.json,即使我想要来自控制器的 JS 文件以及如何从控制器获取 js.erb 文件。

【问题讨论】:

  • 你已经提到了remote: true,为什么还要提到"data-type" =&gt; :js
  • 我在尝试不同的选项。只保留远程:true 不起作用。
  • remote: true 不应在 html 中,而应作为 form_with 参数直接传递,例如 url: companies_path
  • Rails 会处理它并在表单上设置data-remote 属性。
  • 它没有。如果我在 html 哈希中传递它,它只会设置 remote = "true"。我正在使用 Rails 5

标签: javascript jquery ruby-on-rails ruby ajax


【解决方案1】:

它正在发送 html 请求,因为您在提交按钮上提到了链接,其行为类似于 &lt;a href="/compaines"&gt;Create a Company&lt;/a&gt;,并且它在未提交表单的情况下执行了您的操作。

试试这个:-

<%= form_for(@company, url: companies_path_path, :html => { class: "form-inline", id: "new-company-create" },remote: true, method: 'POST') do |f| %>
   <div class= "form-group">
    <%= f.label :nil, 'Company Name:', :class => 'sr-only'%>
    <%= f.text_field :nil, :class => 'form-control-plaintext' %>
  </div> 
  <div class="form-group mx-sm-3"> 
    <%= f.label :name, 'Enter a Company Name :   ' %> 
    <%= f.text_field :name, :class => 'form-control large-input-grp', :placeholder => "Enter any Company Name" %>     
  </div>

  <%= button_tag(type: 'submit', class: "btn btn-default", id: "create_company_main") do %>
    Create a Company
  <% end %>
<%end%>

【讨论】:

  • 我做了一些更改,请再看一下,它正在发送 html 请求,因为您在提交按钮上提到了链接。请尝试这个新代码,希望它会向您的控制器发送 ajax 请求
  • 所以在提交按钮中添加链接就是答案,你应该在你的答案上更新它。顺便说一句,非常感谢!
  • 你的表单一切都很好,但是你的提交按钮表现得像一个外部链接,它可以在不提交表单的情况下创建控制器的操作。答案已更新,谢谢。 :)
猜你喜欢
  • 2015-04-19
  • 1970-01-01
  • 2011-07-15
  • 1970-01-01
  • 2014-02-10
  • 1970-01-01
  • 2017-10-18
  • 2014-05-23
  • 1970-01-01
相关资源
最近更新 更多